簡體   English   中英

如何獲取圖像中的邊框像素或擦除區域

[英]How to get the border pixels or erased area in an image

想象我有下面的圖像,有一個擦除區域,現在我想知道這個區域的邊界像素,我如何在Python中實現它? 一個區域被擦除的圖像

emmm 不運行就放下代碼,以后自己試試。

import numpy as np
import cv2 as cv
im = cv.imread('you_input_image.jpg')
imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
# assume the while area 255.255.255 are what you put manually and you want it removed. 
ret, thresh = cv.threshold(imgray, 254, 255, 0)
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# There might be multiple are with 255. then you need to find the index of the largest contour
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas)
cnt=contours[max_index]
print cnt
# cnt contains all the point in this largest contour

您可以使用 opencv 來執行此操作。 要使用的主要功能是cv2.findContours

在下面用紅色繪制邊框。

import cv2
import numpy as np
from skimage.color import rgb2gray
import matplotlib.pyplot as plt

im = plt.imread('uKTss.jpg')
gray = rgb2gray(im)

contours = cv2.findContours(gray.astype(np.uint8),cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)[-2]

for contour in contours:  
    cv2.drawContours(im, contour, -1, (255, 0, 0), 1)

plt.imshow(im)
plt.show()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM