我有一张布局的图片,我想找出一些彩色的圆圈 这段代码无法识别圆圈
这是我需要识别不同颜色的圆圈的图像
代码:-
import cv2
import numpy as np
img = cv2.imread('test7.png',0)
img = cv2.equalizeHist(img)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,10,param1=50,param2=30,minRadius=0,maxRadius=25)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
但这给我错误 错误
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,10,param1=50,param2=30,minRadius=0,maxRadius=25)
cv2.error: OpenCV(3.4.2) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/hough.cpp:1737: error: (-215:Assertion failed) !_image.empty() && _image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) && (_image.isMat() || _image.isUMat()) in function 'HoughCircles'
我有3张狗,猴子,驴的照片。
我需要将这些狗,猴子,驴的图像放在这些圆圈上,并调整其大小。
这些图片的图片
我想要这样的最终输出。
答案 0 :(得分:1)
您可以尝试通过将图像转换为HSV颜色空间-cv2.COLOR_BGR2HSV
来滤除圈子。然后,您可以使用cv2.inRange()
搜索颜色并将其绘制在蒙版上(每种颜色使用不同的蒙版)
类似这样的东西:
滤除圆后,可以使用cv2.findContours()
搜索轮廓并找到其位置(可以搜索其中心点或极端),以确定应该在图像上的较小位置图片。请注意,我已经手动调整了较小的图像,如果您想对同一张图片使用不同的尺寸,则必须修改代码。另外,如果您想保持透明度,则应使用图像的颜色并更改代码。这只是我如何完成任务的一个示例。
示例代码(不透明且具有相同尺寸的图片):
import cv2
import numpy as np
img = cv2.imread('thermal2.png')
dog = cv2.imread('resize1.jpg')
donkey = cv2.imread('resize2.jpg')
monkey = cv2.imread('resize3.png')
resize1 = cv2.resize(dog, (35, 40))
resize2 = cv2.resize(donkey, (60, 35))
resize3 = cv2.resize(monkey, (40, 40))
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
lower_red = np.array([0,50,50])
upper_red = np.array([10,255,255])
lower_yellow = np.array([30,50,50])
upper_yellow = np.array([50,255,255])
mask_blue = cv2.inRange(hsv, lower_blue, upper_blue)
mask_red = cv2.inRange(hsv, lower_red, upper_red)
mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow)
res_blue = cv2.bitwise_and(img,img, mask=mask_blue)
res_red = cv2.bitwise_and(img,img, mask=mask_red)
res_yellow = cv2.bitwise_and(img,img, mask=mask_yellow)
gray_blue = cv2.cvtColor(res_blue, cv2.COLOR_BGR2GRAY)
gray_red = cv2.cvtColor(res_red, cv2.COLOR_BGR2GRAY)
gray_yellow = cv2.cvtColor(res_yellow, cv2.COLOR_BGR2GRAY)
_,thresh_blue = cv2.threshold(gray_blue,10,255,cv2.THRESH_BINARY)
_,thresh_red = cv2.threshold(gray_red,10,255,cv2.THRESH_BINARY)
_,thresh_yellow = cv2.threshold(gray_yellow,10,255,cv2.THRESH_BINARY)
_, contours_blue, hierarhy1 = cv2.findContours(thresh_blue,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
_, contours_red, hierarhy2 = cv2.findContours(thresh_red,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
_, contours_yellow, hierarhy3 = cv2.findContours(thresh_yellow,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for c in contours_red:
size = cv2.contourArea(c)
if size > 30:
cXY_left = tuple(c[c[:, :, 0].argmin()][0])
cXY_top = tuple(c[c[:, :, 1].argmin()][0])
cX = cXY_left[0]
cY = cXY_top[1]
img[cY:cY+resize1.shape[0], cX:cX+resize1.shape[1]]=resize1
for c in contours_blue:
size = cv2.contourArea(c)
if size > 30:
cXY_left = tuple(c[c[:, :, 0].argmin()][0])
cXY_top = tuple(c[c[:, :, 1].argmin()][0])
cX = cXY_left[0]
cY = cXY_top[1]
img[cY:cY+resize2.shape[0], cX:cX+resize2.shape[1]]=resize2
for c in contours_yellow:
size = cv2.contourArea(c)
if size > 30:
cXY_left = tuple(c[c[:, :, 0].argmin()][0])
cXY_top = tuple(c[c[:, :, 1].argmin()][0])
cX = cXY_left[0]
cY = cXY_top[1]
img[cY:cY+resize3.shape[0], cX:cX+resize3.shape[1]]=resize3
cv2.imshow('blue', res_blue)
cv2.imshow('red', res_red)
cv2.imshow('yellow', res_yellow)
cv2.imshow('img',img)
结果: