0
votes

I am trying to use an image editor (such as MS paint or paint.net) to draw bounding boxes with a fixed color (such as pure red with RGB = 255, 0, 0) on images, and later load the images in python (such as opencv imread) by looking for pixels with this RGB value (or BGR value in 0, 0, 255) so that I can create labels for object detection purposes.

However, after the image is saved and loaded, I don't see any pixels with such RGB or BGR values. Instead, these pixels are within a range of values which may be far from what I specified.

I also tried to use something like this for experiment purpose:

cv2.rectangle(img_arr, (10, 10), (60, 60), (0, 0, 255), thickness=1)

Right after this statement, I do see pixels with values (0, 0, 255). However, when I run cv2.imwrite and then cv2.imread like this:

cv2.imwrite(full_path_name, img_arr)

and later:

 img_arr = cv2.imread(full_path_name)

I noticed in this new img_arr, there is no any pixels with these BGR values any more. What is the problem?

Back to original problem of labeling images for object detection, I don't want to use any tools for labeling as most of them are detecting mouse motions, however, my task of object detection is to detect text areas, which requires very accurate bounding boxes so that the later stages of image segmentation and character recognition won't be too hard. Therefore, I prefer a static way so that the bounding boxes can be adjusted to be accurate and even be reviewed. When they are final, we create labels. Will this idea even work?

Thank you very much!

1
Likely you are using JPEG which is lossy. Try using PNG.Mark Setchell
Following the suggestion of @MarkSetchell. Try to make a segmentation color base using threshold values it is likely that the red values are around 255 but not pure 255...opencv-srf.blogspot.com.au/2010/09/…crodriguezo
@crodriguezo Thank you for answering.Bo Shao
@MarkSetchell Using PNG resolved my problem. Thank you so much!Bo Shao
Glad to be of assistance. I have put it as an answer for all to see without wading through the comments. Good luck with your project!Mark Setchell

1 Answers

2
votes

Be careful when using JPEG as intermediate storage for image processing tasks since it is a lossy format and values may differ when you subsequently read them back.

Consider maybe using lossless PNG format for intermediate storage. Or use NetPBM PGM (greyscale) or PPM (colour) format for a particularly simple format to read and write - though be aware it cannot persist metadata, such as copyrights or EXIF data.