农作物不使用opencv为此图像起作用

发布于 2025-02-04 01:11:47 字数 991 浏览 6 评论 0原文

我需要通过使用OpenCV检测图像的坐标来裁剪以下图像。 我尝试了以下代码,但它无法正常工作。

import cv2

#reading image
image = cv2.imread("input.PNG")

#converting to gray scale
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

#applying canny edge detection
edged = cv2.Canny(image, 10, 250)

#finding contours
(_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
idx = 0
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    if w>50 and h>50:
        idx+=1
        new_img=image[y:y+h,x:x+w]
        #cropping images
        cv2.imwrite("cropped/"+str(idx) + '.png', new_img)
#cv2.imshow("Original Image",image)
#cv2.imshow("Canny Edge",edged)
#cv2.waitKey(0)

输入图像:

“在此处输入图像描述”

输出映像:

i need to crop the below image by detecting co-ordinates of the image using opencv.
i tried below code but it's not working as expected.

import cv2

#reading image
image = cv2.imread("input.PNG")

#converting to gray scale
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

#applying canny edge detection
edged = cv2.Canny(image, 10, 250)

#finding contours
(_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
idx = 0
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    if w>50 and h>50:
        idx+=1
        new_img=image[y:y+h,x:x+w]
        #cropping images
        cv2.imwrite("cropped/"+str(idx) + '.png', new_img)
#cv2.imshow("Original Image",image)
#cv2.imshow("Canny Edge",edged)
#cv2.waitKey(0)

Input Image:

enter image description here

Output image:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷夜 2025-02-11 01:11:48

基于从cv2.boundingRect()返回的坐标裁剪图像不会给您带来所需的结果。您需要获取图像的4个角并相应地将其定向。这也可以使用透视转换矩阵完成

,检测边缘,后来找到轮廓不是正确的方法。您需要找到足够大的轮廓以封闭整个页面。为此,对图像进行二进制并找到最大的外部轮廓。

代码:

# read image and binarize
img = cv2.imread(r'C:\Users\524316\Desktop\Stack\dc.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# find the largest contour
contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
c = max(contours, key = cv2.contourArea)

# To find the 4 corners of the contour
rect = cv2.minAreaRect(c)
input_corners = cv2.boxPoints(rect)
input_corners = np.int0(input_corners)

# We need 4 new points onto which the 4 input points need to be warped
# which will be done on an image with the same size as the input image
ht, wd = img.shape[:2]
output_corners = [[0,0], [wd,0], [wd,ht], [0,ht]]

# converting to float data type
input_corners = np.float32(input_corners)
output_corners = np.float32(output_corners)

# get the transformation matrix
M = cv2.getPerspectiveTransform(input_corners, output_corners)
# perform warping 
warped = cv2.warpPerspective(img, M, (wd, ht))

cv2.imshow('Warped output', warped)

结果:

”在此处输入图像描述”

Cropping the image based on coordinates returned from cv2.boundingRect() will not give you the desired result. You need to obtain the 4 corners of the image and orient it accordingly. This can be done using the perspective transformation matrix

Also, detecting edges and later finding contours is not the right way to go. You need to find a contour large enough to enclose the entire page. To do so, binarize the image and find the largest external contour.

Code:

# read image and binarize
img = cv2.imread(r'C:\Users\524316\Desktop\Stack\dc.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# find the largest contour
contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
c = max(contours, key = cv2.contourArea)

# To find the 4 corners of the contour
rect = cv2.minAreaRect(c)
input_corners = cv2.boxPoints(rect)
input_corners = np.int0(input_corners)

# We need 4 new points onto which the 4 input points need to be warped
# which will be done on an image with the same size as the input image
ht, wd = img.shape[:2]
output_corners = [[0,0], [wd,0], [wd,ht], [0,ht]]

# converting to float data type
input_corners = np.float32(input_corners)
output_corners = np.float32(output_corners)

# get the transformation matrix
M = cv2.getPerspectiveTransform(input_corners, output_corners)
# perform warping 
warped = cv2.warpPerspective(img, M, (wd, ht))

cv2.imshow('Warped output', warped)

Result:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文