Karmar
Karmar

Reputation: 590

check pixel's transparency in OpenCV

How to check is pixel transparent in OpenCV? I have a png image with transparent portions and I want to convert rgb image to hsv and then change hue of pixels. I need that transparent pixels remain transparent after the conversion.

Any help please.

Upvotes: 2

Views: 1611

Answers (2)

user1872847
user1872847

Reputation: 41

You may try GDAL. It is compatible with CV2

These links may be useful.

Reading Raster Data with GDAL

GDAL API Tutorial

import gdal
from gdalconst import *
import numpy as np

ds = gdal.Open('lena.jpg', GA_ReadOnly)
B = ds.GetRasterBand(1)
G = ds.GetRasterBand(2)
R = ds.GetRasterBand(3)
A = ds.GetRasterBand(4) // Alpha

height, width = B.shape
img = np.zeros(height, width, 3)
img[:, :, 0] = B
img[:, :, 1] = G
img[:, :, 2] = R

// Do something you want

ds.GetRasterBand(1).WriteArray(new_B)
ds.GetRasterBand(2).WriteArray(new_G)
ds.GetRasterBand(3).WriteArray(new_R)
// The forth band dose not need to be changed

// Close image, the changes is writen in the source file
ds = None

// Note that I did not test this code

Upvotes: 2

Osiris
Osiris

Reputation: 4185

OpenCV does not support transperancy in images. (before v2.4, I'm not sure about the latest version)

You can try the solution at http://blog.developer.stylight.de/2010/05/how-to-load-alpha-channel-pngs-with.html and rebuild OpenCV, or you can use something like ImageMagick to extract the alpha layer (forum link) as a separate image and load it.

Upvotes: 0

Related Questions