As you can see in the image, the size of the image is 2000*2000. We have to find the distance of the image from all the 4 coordinates. I am trying with opencv contours but on some other image it is not working.

image = cv2.imread(image_full_path)
ori_h, ori_w, _ = image.shape
print("Original height, width-->", ori_h, ori_w)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # grayscale
_, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)  # threshold
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
dilated = cv2.dilate(thresh, kernel, iterations=13)  # dilate
contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)  # get contours

idx = 0
# for each contour found, draw a rectangle around it on original image
# print('length', len(contours))
for contour in contours:
    idx += 1

    # get rectangle bounding contour
    [x, y, w, h] = cv2.boundingRect(contour)
    remaining_height_bottom = ori_h - (y + h)
    remaining_width_right_side = ori_w - (x + w)

    print("Upper Height, Right Width-->", y, x)
    print("Bottom Height, Left Width-->", remaining_height_bottom, remaining_width_right_side)
    print("Image Height, Image Width-->", h, w)

Image size is 2000 * 2000

提前谢谢.如果有人认为提问的方式是错误的,请忽略.我真的需要帮助.

推荐答案

Please find the code below it working for me

img = cv2.imread(file_full_path) # Read in the image and convert to grayscale
ori_h, ori_w, _ = img.shape
scrollbarright = 20
img = img[:, :-scrollbarright]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = 255*(gray < 128).astype(np.uint8)
coords = cv2.findNonZero(gray)
x, y, w, h = cv2.boundingRect(coords)
print("I am ",x,y,w,h)

remaining_height_bottom = ori_h - (y + h)
remaining_width_right_side = ori_w - (x + w)

print("Upper Height, Right Width-->", y, x)
print("Bottom Height, Left Width-->", remaining_height_bottom, remaining_width_right_side)
print("Image Height, Image Width-->", h, w)
print("Original Image Height,Original Image Width-->", ori_h, ori_w)

Python-3.x相关问答推荐

错误2没有这样的文件或目录website_content.txt""

替换Pandas中组下的列值

如何在选中项目时设置QTreeView中整行的 colored颜色 ?

按长度和字母数字对Pandas 数据帧列进行排序

在 Python 中比较和排序列之间的值(带有不匹配列)

如何使用 Selenium Python 连续单击一个按钮直到另一个元素出现?

Pandas 窗口聚合两个排序表

Python:如何从句子/段落中提取地址(非正则表达式方法)?

Python3 AttributeError:列表对象没有属性清除

ImportError:没有名为资源的模块

从 Python2 到 Python3 的这种解包行为的变化是什么?

使用打印时,用+连接是否比用,分隔更有效?

如何将numpy数组图像转换为字节?

是否在未完成初始化的对象上调用了 del?

Python在OrderedDict中 Select 第i个元素

如何在 python 3.x 中禁用 ssl 判断?

如何使用已打开并使用登录凭据登录的浏览器

TypeError:只有整数标量数组可以转换为标量索引

matplotlib - 模块sip没有属性setapi

如何将文档字符串放在 Enums 上?