有什么方法可以从图像中提取扭曲的 rectangle/square 吗?
Is there any way to extract distorted rectangle/square from the image?
这是图像,我想填充此矩形或正方形的边缘,以便我可以使用等高线裁剪它。到目前为止我所做的是我使用 canny 边缘检测器来查找边缘然后使用 bitwise_or 我让这个矩形填充了一点但没有完全填充。如何填充这个矩形或有什么方法可以直接裁剪它?
image = cv2.imread('C:/Users/hp/Desktop/segmentation/test3.jpeg')
img3 = img2 = image.copy()
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
img3 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
lower = np.array([155,25,0])
upper = np.array([179,255,255])
mask = cv2.inRange(image, lower, upper)
edges = cv2.Canny(mask, 1, 255, apertureSize=7)
result = cv2.bitwise_or(edges, mask)
这是提取 Python/OpenCV 中矩形白色像素边界的一种方法。
- 读取输入
- 转换为灰色
- 阈值
- 进行Canny边缘检测
- 获取霍夫线段并画成黑底白字
- 获取白色像素的边界
- 将输入裁剪到边界
输入:
import cv2
import numpy as np
# load image as grayscale
img = cv2.imread('rect_lines.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# threshold
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1]
# apply canny edge detection
edges = cv2.Canny(thresh, 100, 200)
# get hough line segments
threshold = 100
minLineLength = 50
maxLineGap = 20
lines = cv2.HoughLinesP(thresh, 1, np.pi/360, threshold, minLineLength, maxLineGap)
# draw lines
linear = np.zeros_like(thresh)
for [line] in lines:
#print(line)
x1 = line[0]
y1 = line[1]
x2 = line[2]
y2 = line[3]
cv2.line(linear, (x1,y1), (x2,y2), (255), 1)
# get bounds of white pixels
white = np.where(linear==255)
xmin, ymin, xmax, ymax = np.min(white[1]), np.min(white[0]), np.max(white[1]), np.max(white[0])
#print(xmin,xmax,ymin,ymax)
# draw bounding box on input
bounds = img.copy()
cv2.rectangle(bounds, (xmin,ymin), (xmax,ymax), (0,0,255))
# crop the image at the bounds
crop = img[ymin:ymax, xmin:xmax]
# save resulting masked image
cv2.imwrite('rect_lines_edges.png', edges)
cv2.imwrite('rect_lines_hough.png', linear)
cv2.imwrite('rect_lines_bounds.png', bounds)
cv2.imwrite('rect_lines_crop.png', crop)
# display result, though it won't show transparency
cv2.imshow("thresh", thresh)
cv2.imshow("edges", edges)
cv2.imshow("lines", linear)
cv2.imshow("bounds", bounds)
cv2.imshow("crop", crop)
cv2.waitKey(0)
cv2.destroyAllWindows()
精明的边缘:
霍夫线段:
输入边界框:
裁剪图像:
这是图像,我想填充此矩形或正方形的边缘,以便我可以使用等高线裁剪它。到目前为止我所做的是我使用 canny 边缘检测器来查找边缘然后使用 bitwise_or 我让这个矩形填充了一点但没有完全填充。如何填充这个矩形或有什么方法可以直接裁剪它?
image = cv2.imread('C:/Users/hp/Desktop/segmentation/test3.jpeg')
img3 = img2 = image.copy()
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
img3 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
lower = np.array([155,25,0])
upper = np.array([179,255,255])
mask = cv2.inRange(image, lower, upper)
edges = cv2.Canny(mask, 1, 255, apertureSize=7)
result = cv2.bitwise_or(edges, mask)
这是提取 Python/OpenCV 中矩形白色像素边界的一种方法。
- 读取输入
- 转换为灰色
- 阈值
- 进行Canny边缘检测
- 获取霍夫线段并画成黑底白字
- 获取白色像素的边界
- 将输入裁剪到边界
输入:
import cv2
import numpy as np
# load image as grayscale
img = cv2.imread('rect_lines.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# threshold
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1]
# apply canny edge detection
edges = cv2.Canny(thresh, 100, 200)
# get hough line segments
threshold = 100
minLineLength = 50
maxLineGap = 20
lines = cv2.HoughLinesP(thresh, 1, np.pi/360, threshold, minLineLength, maxLineGap)
# draw lines
linear = np.zeros_like(thresh)
for [line] in lines:
#print(line)
x1 = line[0]
y1 = line[1]
x2 = line[2]
y2 = line[3]
cv2.line(linear, (x1,y1), (x2,y2), (255), 1)
# get bounds of white pixels
white = np.where(linear==255)
xmin, ymin, xmax, ymax = np.min(white[1]), np.min(white[0]), np.max(white[1]), np.max(white[0])
#print(xmin,xmax,ymin,ymax)
# draw bounding box on input
bounds = img.copy()
cv2.rectangle(bounds, (xmin,ymin), (xmax,ymax), (0,0,255))
# crop the image at the bounds
crop = img[ymin:ymax, xmin:xmax]
# save resulting masked image
cv2.imwrite('rect_lines_edges.png', edges)
cv2.imwrite('rect_lines_hough.png', linear)
cv2.imwrite('rect_lines_bounds.png', bounds)
cv2.imwrite('rect_lines_crop.png', crop)
# display result, though it won't show transparency
cv2.imshow("thresh", thresh)
cv2.imshow("edges", edges)
cv2.imshow("lines", linear)
cv2.imshow("bounds", bounds)
cv2.imshow("crop", crop)
cv2.waitKey(0)
cv2.destroyAllWindows()
精明的边缘:
霍夫线段:
输入边界框:
裁剪图像: