8

I am trying to detect if the picture(black and white sketch) is colored or not in the room conditions by a mobile camera.

enter image description here

I have been able to get this result

enter image description here

using following code

Mat dest = new Mat (sections[i].rows(),sections[i].cols(),CvType.CV_8UC3);
Mat hsv_image = new Mat (sections[i].rows(),sections[i].cols(),CvType.CV_8UC3);

Imgproc.cvtColor (sections[i],hsv_image,Imgproc.COLOR_BGR2HSV);

List <Mat> rgb = new List<Mat> ();
Core.split (hsv_image, rgb);
Imgproc.equalizeHist (rgb [1], rgb [2]);
Core.merge (rgb, sections[i]);
Imgproc.cvtColor (sections[i], dest, Imgproc.COLOR_HSV2BGR);

Core.split (dest, rgb);

How can I sucessfully find out if the image is colored or not. The color can be any and it has room conditions. Please help me on this as I am beginner to it.

Thanks

Kinght 金
  • 14,440
  • 4
  • 49
  • 62
Aqeel Raza
  • 1,279
  • 2
  • 11
  • 21
  • Don't forget to read https://stackoverflow.com/help/someone-answers for all your answers: you don't seem to have ever *accepted* any of the answers you got. – VonC Jul 20 '19 at 08:47

1 Answers1

17

To process the colorful image in HSV color-space is a good direction. And I split the channels and find the S channel is great. Because S is Saturation(饱和度) of color.

enter image description here

Then threshold the S with thresh of 100, you will get this. enter image description here

It will be easy to separate the colorful region in the threshed binary image.


As @Mark suggest, we can use adaptive thresh other than the fixed one. So, add THRESH_OTSU in the flags.

Core python code is presented as follow:

##(1) read into  bgr-space
img = cv2.imread("test.png")

##(2) convert to hsv-space, then split the channels
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)

##(3) threshold the S channel using adaptive method(`THRESH_OTSU`)  
th, threshed = cv2.threshold(s, 100, 255, cv2.THRESH_OTSU|cv2.THRESH_BINARY)

##(4) print the thresh, and save the result
print("Thresh : {}".format(th))
cv2.imwrite("result.png", threshed)


## >>> Thresh : 85.0

enter image description here

Related answers:

  1. Detect Colored Segment in an image
  2. Edge detection in opencv android
  3. OpenCV C++/Obj-C: Detecting a sheet of paper / Square Detection
Kinght 金
  • 14,440
  • 4
  • 49
  • 62
  • 1
    Good solution! Going a step further... if you take your final, thresholded image and calculate its mean and multiply by 100, you will get the percentage of coloured pixels which will make it easy to determine if the image contains colours or not - which seems to be what the OP is asking for. – Mark Setchell Nov 17 '17 at 09:35
  • Thanks for your suggestion, I add the `THRESH_OTSU` into the flags , the result is OK. – Kinght 金 Nov 17 '17 at 10:53
  • 1
    You are amazing guys. Thanks very much for the response. – Aqeel Raza Nov 17 '17 at 12:19