Opencv Python Cover Image Using Numpy Array

I am using OpenCV 3.1.0-dev and python 2.7.

I am trying to cut out the black appearance of the image that I was stitching. The fight is that there are other pixels in the image, so cv2.findcontours returns a very interesting numpy array.

enter image description here

The first image is what I have, and the second image is the target.

I was wondering if someone knows how to crop a polygon into the smallest square containing the entire image. Blue lines and dots are the paths found by cv2.findContours. Is it possible to find the top left side in a numpy array and the bottom right p0int in a numpy array that I can trim? If so, how to do it.

Here is my current code. I'm trying to find a point to trim usingcnt=contours[0]

import cv2
import numpy as np

img = cv2.imread("/Users/chrisradford/Documents/Research/ImagesToPass/masterToCrop.jpg",1)
grayed = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
(_,thresh) = cv2.threshold(grayed,1,255,cv2.THRESH_BINARY)
result, contours, _= cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
# (x,y) = top left coordinate & w,h = idth and height 
x,y,w,h = cv2.boundingRect(cnt) #good
cropped = img[y:y+h,x:x+w] #good
cv2.drawContours(img,contours,-1,(255,0,0),3)
cv2.imshow("result",img)
cv2.imwrite('/Users/chrisradford/Documents/Research/ImagesToPass/StackOverflow.jpg',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Any help appreciated

+4
1

contours, x y , :

x, y = [], []

for contour_line in contours:
    for contour in contour_line:
        x.append(contour[0][0])
        y.append(contour[0][1])

x1, x2, y1, y2 = min(x), max(x), min(y), max(y)

cropped = img[y1:y2, x1:x2]

(x1, y1) (x2, y2) .

, !

+6

Source: https://habr.com/ru/post/1663371/


All Articles