annie
annie

Reputation: 111

how can i get a black and white image for the following picture?

enter image description here

I want to convert the picture into black and white image accurately where the seeds will be represented by white color and the background as black color. I would like to have it in python opencv code. Please help me out

I got good result for the above picture using the given code below. Now I have another picture for which thresholding doesn't seem to work. How can I tackle this problem. The output i got is in the following picture enter image description here

also, there are some dents in the seeds, which the program takes it as the boundary of the seed which is not a good results like in the picture below. How can i make the program ignore dents. Is masking the seeds a good option in this case. enter image description here

Upvotes: 2

Views: 3039

Answers (2)

Jeru Luke
Jeru Luke

Reputation: 21223

I converted the image from BGR color space to HSV color space.

Then I extracted the hue channel:

enter image description here

Then I performed threshold on it:

enter image description here

Note:

Whenever you face difficulty in certain areas try working in a different color space, the HSV color space being most prominent.

UPDATE:

Here is the code:

import cv2
import numpy as np

filename = 'seed.jpg'
img = cv2.imread(filename)  #---Reading image file---

hsv_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)  #---Converting RGB image to HSV
hue, saturation, value, = cv2.split(hsv_img)   #---Splitting HSV image to 3 channels---

blur = cv2.GaussianBlur(hue,(3,3),0)   #---Blur to smooth the edges---

ret,th = cv2.threshold(blur, 38, 255, 0)   #---Binary threshold---
cv2.imshow('th.jpg',th)

Now you can perform contour operations to highlight your regions of interest also. Try it out!! :)

ANOTHER UPDATE:

I found the contours higher than a certain constraint to get this:

enter image description here

Upvotes: 5

Piglet
Piglet

Reputation: 28974

There are countless ways for image segmentation.

The simplest one is a global threshold operation. If you want to know more on other methods you should read some books. Which I recommend anyway befor you do any further image processing. It doesn't make much sense to start image processing if you don't know the most basic tools.

Just to show you how this could be achieved:

I converted the image from RGB to HSB. I then applied separate global thresholds to the hue and brightness channels to get the best segmentation result for both images. Both binary images were then combined using a pixelwise AND operation. I did this because both channels gave sub-optimal results, but their overlap was pretty good. I also applied some morphological operators to clean up the results. Of course you can just invert the image to get the desired black background...

Thresholds and the used channels of course depend on the image you have and what you want to achieve. This is a very case-specific process that can be dynamically adapted to a limited extend.

enter image description here

This could be followed by labling or whatever you need: enter image description here

Upvotes: 2

Related Questions