I was wanting to make a cut of a square formed by points and then center that cut on an image with opencv in python, I have a problem generating a square around the place where I have the most points.
This is the original picture:
And this is what I get when I want to enclose the part with the most points inside a rectangle:
The rectangle looks like this because there are scattered pixels. This is the code I am using:
import numpy as np
import cv2
img = cv2.imread('123456__.png')
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,64,255,0)
kernel = np.ones((2,2),np.uint8)
img_e = cv2.dilate(thresh,kernel,iterations = 1)
imgbw, contours, hierarchy =
cv2.findContours(img_e,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
margin_distance = 25
def get_min_max_values(cs, im_y, im_x):
min_y = im_y - margin_distance
min_x = im_x - margin_distance
max_y = margin_distance
max_x = margin_distance
for lvl1 in cs:
for lvl2 in lvl1:
x, y = lvl2[0]
# x = im_x - x
# y = im_y - y
max_y = max(y, max_y) if y + margin_distance < im_y else max_y
max_x = max(x, max_x) if x + margin_distance < im_x else max_x
min_y = min(y, min_y) if y > margin_distance else min_y
min_x = min(x, min_x) if x > margin_distance else min_x
return ((min_y, min_x), (min_y, max_x), (max_y, min_x), (max_y, max_x))
new_rect = get_min_max_values(contours, len(img), len(img[0]))
new_rect = list(map(lambda x: list(x)[::-1], list(new_rect)))
rect = cv2.minAreaRect(np.int0(new_rect))
box = cv2.boxPoints(rect)
box = np.int0(box)
img_out = cv2.drawContours(img, [box], -1, (0,0,255), 5) # -1 = wszystkie kontury
img_out = cv2.drawContours(img, contours, -1, (0,255,0), 3)
cv2.imwrite("outssss.png",
Thank you very much in advance for the help!