OpenCV 查找 RGBA 图像的轮廓不起作用

问题描述 投票:0回答:1

输入图像为

Contours_X.png
[Contours_X.png:PNG 图像数据,819 x 1154,8 位灰度,非隔行] :

Python 仅查找轮廓白色区域 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_BGRA2GRAY)
    
    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,非隔行扫描 ] :

使用

input_alpha_2.png
[input_alpha_2.png:PNG 图像数据,1000 x 1200,8 位/颜色 RGBA,逐行扫描] :

和代码:

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_BGRA2GRAY)
    
    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 图像数据,1000 x 1200,8 位/颜色 RGBA,逐行扫描]:

为什么我没有像第一个示例那样在主题周围获得漂亮的绿色边框??

正如评论中所建议的:

您的基础 BGR 图像(在 Alpha 通道下)有绿线。 Alpha 通道覆盖了它)。删除 Alpha 通道即可看到它。

并用 :

来做到这一点
cv.imwrite("image.png", image[:,:,:3])

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

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

python opencv image-processing
1个回答
0
投票

我的 python/opencv 目前坏了。所以如果我拍你的透明图像

并删除 Imagemagick 中的 Alpha 通道,我明白了

convert image.png -alpha off x.png

© www.soinside.com 2019 - 2024. All rights reserved.