输入图像为Contours_X.png[Contors_X.png:PNG图像数据,819 x 1154,8位灰度,非隔行扫描]:

enter image description here

Python Find Contours white region only OpenCV个代码中窃取代码:

import cv2 as cv
import numpy as np

def generate_X_Y(image_path):
    
    image = cv.imread(image_path)
    
    # image = cv.imread(image_path, cv.IMREAD_UNCHANGED)
    
    cv.imwrite("image_ori.png" , image)
    
    print('image[0] : ', image[0])
    
    gray = cv.cvtColor(image, cv.COLOR_RGBA2GRAY)
    
    print('gray[0] : ', gray[0])
    
    ## CHANGED TO:
    ret, thresh = cv.threshold(gray, 128, 255, cv.THRESH_BINARY_INV)
    
    cv.imwrite("image2.png", thresh)
    
    contours, hierarchies = cv.findContours(thresh, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
    blank = np.zeros(thresh.shape[:2], dtype='uint8')
    cv.drawContours(blank, contours, -1, (255, 0, 0), 1)
    cv.imwrite("Contours.png", blank)
    
    print('len(contours) : ' , len(contours))
    
    for i in contours:
        
        cv.drawContours(image, [i], -1, (0, 255, 0), 2)
        
    cv.imwrite("image.png", image)
    

if __name__ == '__main__':
    
    image_path = 'Contours_X.png'  # Provide the correct path in Colab
    
    # image_path = 'input_alpha.png' 
    
    generate_X_Y(image_path)

我得到输出image.png[Image.png:PNG图像数据,819 x 1154,8位/彩色RGB,非隔行扫描 ]:

enter image description here

使用input_alpha_2.png[Input_Alpha_2.png:PNG图像数据,input_alpha_2.png0 x 1200,8位/彩色RGBA,非隔行扫描]:

enter image description here

enter image description here

和代码:

import cv2 as cv
import numpy as np


def generate_X_Y(image_path):
    
    # image = cv.imread(image_path)
    
    image = cv.imread(image_path, cv.IMREAD_UNCHANGED)
    
    cv.imwrite("image_ori.png" , image)
    
    print('image[0] : ', image[0])
    
    gray = cv.cvtColor(image, cv.COLOR_RGBA2GRAY)
    
    print('gray[0] : ', gray[0])
    
    ## CHANGED TO:
    ret, thresh = cv.threshold(gray, 128, 255, cv.THRESH_BINARY_INV)
    
    cv.imwrite("image2.png", thresh)
    
    contours, hierarchies = cv.findContours(thresh, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
    blank = np.zeros(thresh.shape[:2], dtype='uint8')
    cv.drawContours(blank, contours, -1, (255, 0, 0), 1)
    cv.imwrite("Contours.png", blank)
    
    print('len(contours) : ' , len(contours))
    
    for i in contours:
        
        cv.drawContours(image, [i], -1, (0, 255, 0), 20)
        

    cv.imwrite("image.png", image)
    

if __name__ == '__main__':
    
    # image_path = 'Contours_X.png'  # Provide the correct path in Colab
    
    image_path = 'input_alpha_2.png' 
    
    generate_X_Y(image_path)

我得到image.png[Image.png:PNG图像数据,image.png0 x 1200,8位/彩色RGBA,非隔行扫描]:

enter image description here

enter image description here

为什么我不像第一个例子那样在主题周围画一个漂亮的绿色边框?

正如 comments 中所建议的:

您的基本BGR图像(在Alpha通道下)具有您的绿色线条.Alpha通道正在覆盖它).移除Alpha通道以查看它.

并做到这一点:

cv.imwrite("image.png", image[:,:,:3])

我得到image.png: PNG image data, 1000 x 1200, 8-bit/color RGBA, non-interlaced分:

enter image description here

但我仍然不明白透明的Alpha通道如何隐藏轮廓,为什么我会得到灰色背景,我相信这可能是我图像中最大轮廓的区域,即方形外部黑色边框?

有关这方面的更多信息,请使用:

cntsSorted = sorted(contours, key = lambda x: cv.contourArea(x) , reverse=True)
    
for index , i in enumerate(cntsSorted) :
        
    print(cv.contourArea(i))
        
    if index > 0 :

        cv.drawContours(image, [i], -1, (0, 255, 0), 20)
  
cv.imwrite("image.png", image)    

cv.imwrite("image_rem.png", image[:,:,:3])
    

第二幅图像没有更多的外部绿色边框,但仍然保持深灰色背景.

推荐答案

另一种在图像上绘制轮廓的方法是在轮廓绘制中指定四个通道,因为它有四个通道:

im = cv2.imread("disneyRGBA.png", cv2.IMREAD_UNCHANGED)
imContoured = im.copy()
imGray = cv2.cvtColor(im, cv2.COLOR_RGBA2GRAY)
ret, thresh = cv2.threshold(imGray, 128, 255, cv2.THRESH_BINARY_INV)
contours, hierarchies = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# I copied this from you, but you could have easily went with cv2.drawContours(imContoured, contours, -1, (0, 255, 0, 255), 20)
for i in contours:
    imContoured = cv2.drawContours(imContoured, [i], -1, (0, 255, 0, 255), 20)

以下是在Alpha通道上写入等高线为255的图像后的结果:

255 on alpha channel

以下是在Alpha通道上写入等高线为0的图像后的结果:

0 on alpha channel

来自Adobe Photoshop的同一张图片:

enter image description here

使用:

imContoured = cv2.drawContours(imContoured, [i], -1, (0, 255, 0, 0), 100)

Alpha通道上的0和较粗的轮廓(100):

enter image description here

以下是在Alpha通道上写入带127等高线的图像后的结果:

127 on alpha

不知何故,我以为反过来,255是完全透明的,0是不透明的.但情况似乎正好相反.

Python相关问答推荐

如何并行化/加速并行numba代码?

从嵌套的yaml创建一个嵌套字符串,后面跟着点

如何保持服务器发送的事件连接活动?

什么是合并两个embrame的最佳方法,其中一个有日期范围,另一个有日期没有任何共享列?

在嵌套span下的span中擦除信息

matplotlib + python foor loop

如何使用使用来自其他列的值的公式更新一个rabrame列?

(Python/Pandas)基于列中非缺失值的子集DataFrame

循环浏览每个客户记录,以获取他们来自的第一个/最后一个渠道

Python—在嵌套列表中添加相同索引的元素,然后计算平均值

你能把函数的返回类型用作其他地方的类型吗?'

Python协议不兼容警告

极点替换值大于组内另一个极点数据帧的最大值

如何从数据框列中提取特定部分并将该值填充到其他列中?

python3中np. divide(x,y)和x/y有什么区别?'

对数据帧进行分组,并按组间等概率抽样n行

如何在PYTHON中向单元测试S Side_Effect发送额外参数?

如何在Polars中将列表中的新列添加到现有的数据帧中?

ValueError:必须在Pandas 中生成聚合值

将鼠标悬停在海运`pairplot`的批注/高亮显示上