使用 OpenCV 检测 squares/rectangles
Detecting squares/rectangles using OpenCV
所以我试图找到图像上的每个正方形,但检测到太多。我只想检测大立方体,而不是周围的所有噪音。
我的问题也是立方体可以有不同的颜色,但它们的大小总是相同的。
我写了一些代码,但是没有用。
import numpy as np
import cv2
img = cv2.imread('gulRec.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imagem = (255-gray)
ret,thresh = cv2.threshold(imagem,120,200,1)
contours,h = cv2.findContours(thresh,1,2)
for cnt in contours:
approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
print (len(approx))
if len(approx)==4:
cv2.drawContours(img,[cnt],0,(0,0,255),-1)
cv2.imshow('thresh',thresh)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
这是检测 Python/OpenCV 中红色和黄色立方体的一种方法。红色和黄色立方体的基本思想是认识到红色和黄色的色调是所有颜色中最低的。因此可以转换为 HSV 并使用 cv2.inRange() 来设置红色和黄色的阈值。
输入:
import cv2
import numpy as np
img = cv2.imread("4cubes.jpg")
# convert to HSV, since red and yellow are the lowest hue colors and come before green
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# create a binary thresholded image on hue between red and yellow
lower = (0,240,160)
upper = (30,255,255)
thresh = cv2.inRange(hsv, lower, upper)
# apply morphology
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
clean = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15,15))
clean = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
# get external contours
contours = cv2.findContours(clean, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
result1 = img.copy()
result2 = img.copy()
for c in contours:
cv2.drawContours(result1,[c],0,(0,0,0),2)
# get rotated rectangle from contour
rot_rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rot_rect)
box = np.int0(box)
# draw rotated rectangle on copy of img
cv2.drawContours(result2,[box],0,(0,0,0),2)
# save result
cv2.imwrite("4cubes_thresh.jpg",thresh)
cv2.imwrite("4cubes_clean.jpg",clean)
cv2.imwrite("4cubes_result1.png",result1)
cv2.imwrite("4cubes_result2.png",result2)
# display result
cv2.imshow("thresh", thresh)
cv2.imshow("clean", clean)
cv2.imshow("result1", result1)
cv2.imshow("result2", result2)
cv2.waitKey(0)
cv2.destroyAllWindows()
阈值图像:
形态学清理图像:
轮廓:
旋转边界框:
所以我试图找到图像上的每个正方形,但检测到太多。我只想检测大立方体,而不是周围的所有噪音。 我的问题也是立方体可以有不同的颜色,但它们的大小总是相同的。
我写了一些代码,但是没有用。
import numpy as np
import cv2
img = cv2.imread('gulRec.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imagem = (255-gray)
ret,thresh = cv2.threshold(imagem,120,200,1)
contours,h = cv2.findContours(thresh,1,2)
for cnt in contours:
approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
print (len(approx))
if len(approx)==4:
cv2.drawContours(img,[cnt],0,(0,0,255),-1)
cv2.imshow('thresh',thresh)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
这是检测 Python/OpenCV 中红色和黄色立方体的一种方法。红色和黄色立方体的基本思想是认识到红色和黄色的色调是所有颜色中最低的。因此可以转换为 HSV 并使用 cv2.inRange() 来设置红色和黄色的阈值。
输入:
import cv2
import numpy as np
img = cv2.imread("4cubes.jpg")
# convert to HSV, since red and yellow are the lowest hue colors and come before green
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# create a binary thresholded image on hue between red and yellow
lower = (0,240,160)
upper = (30,255,255)
thresh = cv2.inRange(hsv, lower, upper)
# apply morphology
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
clean = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15,15))
clean = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
# get external contours
contours = cv2.findContours(clean, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
result1 = img.copy()
result2 = img.copy()
for c in contours:
cv2.drawContours(result1,[c],0,(0,0,0),2)
# get rotated rectangle from contour
rot_rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rot_rect)
box = np.int0(box)
# draw rotated rectangle on copy of img
cv2.drawContours(result2,[box],0,(0,0,0),2)
# save result
cv2.imwrite("4cubes_thresh.jpg",thresh)
cv2.imwrite("4cubes_clean.jpg",clean)
cv2.imwrite("4cubes_result1.png",result1)
cv2.imwrite("4cubes_result2.png",result2)
# display result
cv2.imshow("thresh", thresh)
cv2.imshow("clean", clean)
cv2.imshow("result1", result1)
cv2.imshow("result2", result2)
cv2.waitKey(0)
cv2.destroyAllWindows()
阈值图像:
形态学清理图像:
轮廓:
旋转边界框: