下图如何获得黑白图像? [英] how can i get a black and white image for the following picture?
问题描述
我想将图片准确地转换为黑白图像,其中种子将用白色表示,背景用黑色表示.我想在python opencv代码中使用它.请帮帮我
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
此外,种子中还有一些凹痕,程序将其作为种子的边界,效果不佳,如下图所示.我如何使该程序忽略凹痕.在这种情况下,遮盖种子是个不错的选择.
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.
推荐答案
我将图像从BGR颜色空间转换为HSV颜色空间.
I converted the image from BGR color space to HSV color space.
然后我提取了色调频道:
然后我对其执行了阈值:
Then I performed threshold on it:
注意:
每当您在某些区域遇到困难时,请尝试在不同的色彩空间中工作,HSV色彩空间最为突出.
Whenever you face difficulty in certain areas try working in a different color space, the HSV color space being most prominent.
更新:
这是代码:
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!! :)
其他更新:
我发现轮廓高于某个约束以得到此结果:
I found the contours higher than a certain constraint to get this:
这篇关于下图如何获得黑白图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!