使用 OpenCV 进行霍夫变换的裁剪圆 Python
Cropping Circle with Hough Transform using OpenCV Python
我想从下面的虹膜图像中裁剪圆圈:
这是我的圆检测代码:
from matplotlib import pyplot as plt
import numpy as np
import cv2
gambar = cv2.imread('tes2.jpg',0)
cimg = cv2.cvtColor(gambar,cv2.COLOR_GRAY2BGR)
canny = cv2.Canny(cimg,50,50)
circles = cv2.HoughCircles(canny,cv2.HOUGH_GRADIENT,1,10000,
param1=50,param2=30,minRadius=50,maxRadius=200)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(cimg,(i[0],i[2]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(cimg,(i[0],i[2]),2,(0,0,255),3)
plt.imshow(cimg,cmap = 'gray')
plt.show()
但是,我不知道如何裁剪这个圆圈(虹膜定位)。我一直在关注此代码参考 。这是我下面的裁剪图像代码:
from matplotlib import pyplot as plt
import numpy as np
import cv2
gambar1 =cv2.imread('tes2.jpg')
gambar = cv2.imread('tes2.jpg',0)
cimg = cv2.cvtColor(gambar,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(cimg, 50, 255, cv2.THRESH_BINARY)
# Create mask
height,width = gambar.shape
mask = np.zeros((height,width), np.uint8)
canny = cv2.Canny(thresh,100,200)
gray = cv2.cvtColor(gambar,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(canny,cv2.HOUGH_GRADIENT,1,10000,
param1=50,param2=30,minRadius=0,maxRadius=0)
for i in circles[0,:]:
cv2.circle(mask,(i[0],i[1]),i[2],(255,255,255),thickness=-1)
masked_data = cv2.bitwise_and(gambar1, gambar1, mask=mask)
# Apply Threshold
_,thresh = cv2.threshold(mask,1,255,cv2.THRESH_BINARY)
# Find Contour
contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
x,y,w,h = cv2.boundingRect(contours[0])
# Crop masked_data
crop = masked_data[y:y+h,x:x+w]
plt.imshow(gambar1,cmap = 'gray')
plt.imshow(crop, cmap='gray')
plt.show()
但是,当我尝试使用上述参考代码时,出现如下错误:
File "C:/Users/zurri/spyder/masktes.py", line 14, in <module>
cimg = cv2.cvtColor(gambar,cv2.COLOR_BGR2GRAY)
error: OpenCV(4.2.0) c:\projects\opencv-python\opencv\modules\imgproc\src\color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function '__cdecl cv::impl::`anonymous-namespace'::CvtHelper<struct cv::impl::`anonymous namespace'::Set<3,4,-1>,struct cv::impl::A0xe227985e::Set<1,-1,-1>,struct cv::impl::A0xe227985e::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
Invalid number of channels in input image:
'VScn::contains(scn)'
where
'scn' is 1
谁能帮我解决这个问题?我只想使用 openCV 裁剪这个圆圈(虹膜定位)python,谢谢
说明
findContours
方法使用 Suzuki's 算法从给定的二值图像中检索轮廓。轮廓对于形状分析、对象检测和识别很有用。
findContour
方法returns两个变量:contours
和hierarchy
。在原始代码中,您执行了 contours = cv2.findContour(...)
,其中组合了 contours
和 hierarchy
变量。因此你有一个错误。我们根本不需要 hieararchy
变量,因此我们放置了 _
。所以错误解决了。
解决方案
将 findContours
行替换为以下内容:
contours, _ = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
结果:
我想从下面的虹膜图像中裁剪圆圈:
这是我的圆检测代码:
from matplotlib import pyplot as plt
import numpy as np
import cv2
gambar = cv2.imread('tes2.jpg',0)
cimg = cv2.cvtColor(gambar,cv2.COLOR_GRAY2BGR)
canny = cv2.Canny(cimg,50,50)
circles = cv2.HoughCircles(canny,cv2.HOUGH_GRADIENT,1,10000,
param1=50,param2=30,minRadius=50,maxRadius=200)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(cimg,(i[0],i[2]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(cimg,(i[0],i[2]),2,(0,0,255),3)
plt.imshow(cimg,cmap = 'gray')
plt.show()
但是,我不知道如何裁剪这个圆圈(虹膜定位)。我一直在关注此代码参考
from matplotlib import pyplot as plt
import numpy as np
import cv2
gambar1 =cv2.imread('tes2.jpg')
gambar = cv2.imread('tes2.jpg',0)
cimg = cv2.cvtColor(gambar,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(cimg, 50, 255, cv2.THRESH_BINARY)
# Create mask
height,width = gambar.shape
mask = np.zeros((height,width), np.uint8)
canny = cv2.Canny(thresh,100,200)
gray = cv2.cvtColor(gambar,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(canny,cv2.HOUGH_GRADIENT,1,10000,
param1=50,param2=30,minRadius=0,maxRadius=0)
for i in circles[0,:]:
cv2.circle(mask,(i[0],i[1]),i[2],(255,255,255),thickness=-1)
masked_data = cv2.bitwise_and(gambar1, gambar1, mask=mask)
# Apply Threshold
_,thresh = cv2.threshold(mask,1,255,cv2.THRESH_BINARY)
# Find Contour
contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
x,y,w,h = cv2.boundingRect(contours[0])
# Crop masked_data
crop = masked_data[y:y+h,x:x+w]
plt.imshow(gambar1,cmap = 'gray')
plt.imshow(crop, cmap='gray')
plt.show()
但是,当我尝试使用上述参考代码时,出现如下错误:
File "C:/Users/zurri/spyder/masktes.py", line 14, in <module>
cimg = cv2.cvtColor(gambar,cv2.COLOR_BGR2GRAY)
error: OpenCV(4.2.0) c:\projects\opencv-python\opencv\modules\imgproc\src\color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function '__cdecl cv::impl::`anonymous-namespace'::CvtHelper<struct cv::impl::`anonymous namespace'::Set<3,4,-1>,struct cv::impl::A0xe227985e::Set<1,-1,-1>,struct cv::impl::A0xe227985e::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
Invalid number of channels in input image:
'VScn::contains(scn)'
where
'scn' is 1
谁能帮我解决这个问题?我只想使用 openCV 裁剪这个圆圈(虹膜定位)python,谢谢
说明
findContours
方法使用 Suzuki's 算法从给定的二值图像中检索轮廓。轮廓对于形状分析、对象检测和识别很有用。
findContour
方法returns两个变量:contours
和hierarchy
。在原始代码中,您执行了 contours = cv2.findContour(...)
,其中组合了 contours
和 hierarchy
变量。因此你有一个错误。我们根本不需要 hieararchy
变量,因此我们放置了 _
。所以错误解决了。
解决方案
将 findContours
行替换为以下内容:
contours, _ = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
结果: