2

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

annie
  • 111
  • 2
  • 10
  • I tried doing all types of thresholding but the results are not good due to low difference in the background and foreground. – annie Mar 14 '17 at 06:49
  • threshold did not work? – Jeru Luke Mar 14 '17 at 07:09
  • there are many ways of creating a black and white (binary) image from your input. this process is called segmentation. they reach from simple global thresholds to complex machine learning. Please do yourself a favour and read a fundamentals book on image processing befor you continue doing such stuff... – Piglet Mar 14 '17 at 13:10
  • Calculate edges -> perform morphological dilation -> Masking (or) contour properties – Jeru Luke Mar 14 '17 at 21:39
  • also try inverting the binary image before fining contours – Jeru Luke Mar 15 '17 at 08:56

2 Answers2

5

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

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
2

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

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • Nice work!! How were you able to just narrow in on the objects of interest? – Jeru Luke Mar 14 '17 at 13:59
  • 1
    @JeruLuke I just cropped the images manually. But you could easily remove the ruler lines morphologically or by a minimum area constraint a following labeling step. But usually you know where your stuff is so a fixed region of interest is pretty much always an option. – Piglet Mar 14 '17 at 14:10
  • Minimum constraint area sounds best in this case. Nice work anyway :D – Jeru Luke Mar 14 '17 at 14:12
  • @Piglet wow ..this looks awesome...can you please share the code? Can you also mention any book title to me please? – annie Mar 14 '17 at 14:38
  • @Piglet This is exactly what i need. Would you please share the code? – annie Mar 14 '17 at 14:49
  • I clicked this together in ImageJ (great tool for testing things quickly). The algorithms used are available in any decent image processing library. Programming it from scratch would be also quite trivial though. For beginners I can recommend any books from Burger & Burge or Gonzales & Woods. – Piglet Mar 14 '17 at 15:31
  • @Piglet what are the threshold values you used for global thresholding on hue and value channels? – annie Mar 19 '17 at 01:48
  • @annie I have no idea. Just use one that gives you the best segmentation. If you don't have an interactive tool that gives you a preview you should get one. That's more efficient than trying hardcoded values. – Piglet Mar 19 '17 at 11:01