Python 如何在图像中检测到的斑点周围绘制红色圆圈?
我有以下图像: 我希望在产出方面取得3项成果:Python 如何在图像中检测到的斑点周围绘制红色圆圈?,python,image,opencv,image-processing,object-detection,Python,Image,Opencv,Image Processing,Object Detection,我有以下图像: 我希望在产出方面取得3项成果: 突出显示图像中的黑点/补丁,周围有红色圆形轮廓 计算点/面片的数量 打印覆盖在图像上的点/面片的数量 现在,我只能计算图像中的点/面片数并打印出来: import cv2 ## convert to grayscale gray = cv2.imread("blue.jpg", 0) ## threshold th, threshed = cv2.threshold(gray, 100, 255,cv2.THRESH_BINARY_INV|c
import cv2
## convert to grayscale
gray = cv2.imread("blue.jpg", 0)
## threshold
th, threshed = cv2.threshold(gray, 100, 255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)
## findcontours
cnts = cv2.findContours(threshed, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2]
## filter by area
s1= 3
s2 = 20
xcnts = []
for cnt in cnts:
if s1<cv2.contourArea(cnt) <s2:
xcnts.append(cnt)
print("Number of dots: {}".format(len(xcnts)))
>>> Number of dots: 66
导入cv2
##转换为灰度
灰色=cv2.imread(“blue.jpg”,0)
##门槛
th,threshed=cv2.阈值(灰色,100255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
##findcontours
cnts=cv2.findContours(脱粒,cv2.RETR\u列表,cv2.CHAIN\u近似简单)[-2]
##按面积过滤
s1=3
s2=20
xcnts=[]
对于cnt中的cnt:
如果s1>点数:66
但我不知道如何突出显示图像上的补丁
编辑:以下图像的预期结果:
是这样的:
drawContours()、convexHull()或MineConclosingCircle()应能满足您的需要。
以下是来自opencv的教程,介绍了如何执行您想要执行的操作:
OpenCV有很多很棒的教程,所以当你想学习一些新的东西时,首先检查它们:)正如@alkasm先生所说,你可以使用
cv2.drawcours()
。因此,您可以在代码末尾添加以下内容:
image = cv2.imread("blue.jpg")
cv2.drawContours(image, cnts,
contourIdx = -1,
color = (0, 255, 0), #green
thickness = 5)
cv2.imshow('Contours', image)
cv2.waitKey()
现在,图像将如下所示:
以下是一些方法: 1。颜色阈值化 其思想是将图像转换为HSV格式,然后定义一个较低和较高的颜色阈值,以隔离所需的颜色范围。这将生成一个遮罩,在该遮罩中,我们可以使用找到遮罩上的轮廓,并使用绘制轮廓 2。简单阈值化 其思想是设置阈值并获得二进制掩码。类似地,为了突出显示图像中的面片,我们使用
cv2.drawContours()
。为了确定菌落的数量,我们在遍历轮廓时保留一个计数器。最后,为了在图像上打印补丁的数量,我们使用
殖民地:11
检测蓝色斑点的颜色阈值也会起作用
lower = np.array([0, 0, 0])
upper = np.array([179, 255, 84])
您可以使用此脚本确定HSV下限和上限颜色范围
import cv2
import sys
import numpy as np
def nothing(x):
pass
# Load in image
image = cv2.imread('1.jpg')
# Create a window
cv2.namedWindow('image')
# create trackbars for color change
cv2.createTrackbar('HMin','image',0,179,nothing) # Hue is from 0-179 for Opencv
cv2.createTrackbar('SMin','image',0,255,nothing)
cv2.createTrackbar('VMin','image',0,255,nothing)
cv2.createTrackbar('HMax','image',0,179,nothing)
cv2.createTrackbar('SMax','image',0,255,nothing)
cv2.createTrackbar('VMax','image',0,255,nothing)
# Set default value for MAX HSV trackbars.
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)
# Initialize to check if HSV min/max value changes
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0
output = image
wait_time = 33
while(1):
# get current positions of all trackbars
hMin = cv2.getTrackbarPos('HMin','image')
sMin = cv2.getTrackbarPos('SMin','image')
vMin = cv2.getTrackbarPos('VMin','image')
hMax = cv2.getTrackbarPos('HMax','image')
sMax = cv2.getTrackbarPos('SMax','image')
vMax = cv2.getTrackbarPos('VMax','image')
# Set minimum and max HSV values to display
lower = np.array([hMin, sMin, vMin])
upper = np.array([hMax, sMax, vMax])
# Create HSV Image and threshold into a range.
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
output = cv2.bitwise_and(image,image, mask= mask)
# Print if there is a change in HSV value
if( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
phMin = hMin
psMin = sMin
pvMin = vMin
phMax = hMax
psMax = sMax
pvMax = vMax
# Display output image
cv2.imshow('image',output)
# Wait longer to prevent freeze for videos.
if cv2.waitKey(wait_time) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
使用
drawContours()
请添加第二张显示预期结果的标记图像。谢谢。@MarkSetchell编辑后包含一个示例输出。@Kristada673,提供的图像将以课程的灰度显示。这将打开两个窗口,一个窗口显示原始图像和阈值跟踪器,另一个窗口显示高亮显示的圆。我希望它在同一个窗口中-即,突出显示补丁的原始图像。只需在原始图像上绘制圆,而不是使用绘图。cv.drawContours(绘图,轮廓,多边形,I,颜色)cv.circle(绘图,(int(中心[I][0]),int(中心[I][1]),int(半径[I]),颜色,2)
将此处的绘图更改为原始图像的名称通过这种方式,我可以获得原始图像上的高光,但“跟踪器”窗口仍会单独显示。如何获取cv.createTrackbar()
以在同一原始窗口中创建轨迹栏?是否需要轨迹栏?或者您只是复制了示例中的所有代码?要将其添加到原始图像中,您可以使用source\u window='source'cv.namedWindow(source\u window)cv.imshow(source\u window,src)
,然后使用cv.createTrackbar('Canny thresh:',source\u window,thresh,max\u thresh,thresh\u callback)
将它们指向您想要轨迹条的窗口。要避免出现另一个窗口,您只需避免创建或显示它。是否有任何方法调整阈值,使较暗的点也高亮显示?请检查此项。。。希望这就是您想要的:)如何突出显示较小的补丁而不是较大的补丁?例如:使用具有最小/最大阈值区域的cv2.contourArea()
。如果轮廓通过此过滤器,则高亮显示它,否则忽略它。类似于maximum=500
,如果cv2.contourArea(c)
则高亮显示
lower = np.array([0, 0, 0])
upper = np.array([179, 255, 84])
import cv2
import sys
import numpy as np
def nothing(x):
pass
# Load in image
image = cv2.imread('1.jpg')
# Create a window
cv2.namedWindow('image')
# create trackbars for color change
cv2.createTrackbar('HMin','image',0,179,nothing) # Hue is from 0-179 for Opencv
cv2.createTrackbar('SMin','image',0,255,nothing)
cv2.createTrackbar('VMin','image',0,255,nothing)
cv2.createTrackbar('HMax','image',0,179,nothing)
cv2.createTrackbar('SMax','image',0,255,nothing)
cv2.createTrackbar('VMax','image',0,255,nothing)
# Set default value for MAX HSV trackbars.
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)
# Initialize to check if HSV min/max value changes
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0
output = image
wait_time = 33
while(1):
# get current positions of all trackbars
hMin = cv2.getTrackbarPos('HMin','image')
sMin = cv2.getTrackbarPos('SMin','image')
vMin = cv2.getTrackbarPos('VMin','image')
hMax = cv2.getTrackbarPos('HMax','image')
sMax = cv2.getTrackbarPos('SMax','image')
vMax = cv2.getTrackbarPos('VMax','image')
# Set minimum and max HSV values to display
lower = np.array([hMin, sMin, vMin])
upper = np.array([hMax, sMax, vMax])
# Create HSV Image and threshold into a range.
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
output = cv2.bitwise_and(image,image, mask= mask)
# Print if there is a change in HSV value
if( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
phMin = hMin
psMin = sMin
pvMin = vMin
phMax = hMax
psMax = sMax
pvMax = vMax
# Display output image
cv2.imshow('image',output)
# Wait longer to prevent freeze for videos.
if cv2.waitKey(wait_time) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()