我找不到在新图像零点上拟合轮廓的方法 [英] I can't find a way to fit contour on new image zero point

查看:139
本文介绍了我找不到在新图像零点上拟合轮廓的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制图像,我想找到轮廓,以将最大的轮廓拟合到具有轮廓大小的新图像中,就像矩形周围一样.换句话说,是将轮廓适合较小尺寸的新图像.

I have a binary image and I want to find contours, to fit the biggest one into a new image with the size of the contour as if a rectangle was around it. In other words, to fit the contour into a new image with lower size.

查找轮廓例程正在为整个图像查找一个矩形,我不需要它.我看着尺寸(宽-1,高-1)的轮廓,然后跳过它.

The find contours routine is finding a rectangle for the whole image, and I don't need it. I look a contour of dimension (width - 1, height - 1) and skip it.

我要删除最大的矩形,然后将第二个最大的矩形放入新图像中.那个最大的矩形将限制新图像的显示.然后我想将轮廓绘制成新的白色图像.

I want to remove biggest rectangle and then fit the 2nd biggest rectangle into a new image. That biggest rectangle will make the limits of the new image. Then I want to draw contours into a new white image.

我对OpenCV以及做到这一点的最佳方法的了解还不够.

I just don't know enough about OpenCV and the best way of doing this.

h = img.shape[0]
w = img.shape[1]
ret, img = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)
# are these the best find contours params?
contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# paint a new image white
img = np.zeros((384, 640, 1), np.uint8)
img[:-1] = 255
# resize the contours
for i in range(0, len(contours)):
      for j in range(0, len(contours[i])):
         for k in range(0, len(contours[i][j])):
            if contours[i][j][k][1] != h - 1 or contours[i][j][k][0] != w -1:
               contours[i][j][k][1] = 384 * contours[i][j][k][1] / h
               contours[i][j][k][0] = 640 * contours[i][j][k][0] / w

我找不到找到整个文档矩形的方法.最大的矩形是图像宽度*高度,但是在第二个矩形中,只有黑色像素可见.

I can't find a way of finding the rectangle for the whole document. The biggest rectangle is image width * height, but in the 2nd one, only black pixels are visible.

推荐答案

在注释中,您声明希望将黑色像素作为图像的边界.在这种情况下,您可以使用以下方法.它将图像加载为灰度,然后将其反转.因此,原始图像中的白色现在变为黑色(值:0),而黑色变为白色(值:255).接下来,汇总所有行和列.总和大于零的第一行和最后一行/列是原始图像中黑色像素的边界.您可以使用这些值来切片新图像.

In the comments you state that you want the black pixels as the bounds of the image. In that case you can use the method below. It loads the image as grayscale and then inverts it. So the white in the original image is now black (value: 0) and the black becomes white (value: 255). Next all rows and columns are summed up. The first and last rows/columns that have a sum that is greater than zero are the bounds of the black pixels in the original image. YOu can use these values to slice a new image.

结果:

代码:

    import cv2
    import numpy as np
    # load the image as grayscale
    img = cv2.imread('mQTiR.png',0)
    #invert the image
    img_inv = cv2.bitwise_not(img)
    # sum each row and each column of the inverted image
    sumOfCols = np.sum(img_inv, axis=0)
    sumOfRows = np.sum(img_inv, axis=1)
    # get the indexes of the rows/cols that are nonzero (=black in scan)
    nonzeroX = np.nonzero(sumOfCols)[0]
    nonzeroY = np.nonzero(sumOfRows)[0]
    # get the first and last indexes, these are the bounds of the roi
    minY = nonzeroY[0]
    maxY = nonzeroY[-1]
    minX = nonzeroX[0]
    maxX = nonzeroX[-1]
    #create subimage
    subimage = img[minY:maxY,minX:maxX]
    #display subimage
    cv2.imshow('Result',subimage)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

这篇关于我找不到在新图像零点上拟合轮廓的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆