English 中文(简体)
使用 OpenCv 保存时图像透明度暗化
原标题:Image transparency darkened when saved using OpenCv

我创建了一个绘图程序, 允许用户在其中绘制图像并保存图像, 然后再重新装入, 以便继续绘制。 基本上, 我将绘图作为位图传送到要保存的 JNI 层, 并装入上一张图 。

我使用 OpenCv 写入并读取到 png 文件 。

我注意到一些关于图像转换的怪异之处。 似乎透明性是用 OpenCv 上的黑颜色来计算的。 看一下附着的图像, 包含有转换的线条 。

Correct transparency by passing int array to native code, no color conversion needed: enter image description here

Darkened transparency by passing Bitmap object to native code, color conversion needed: enter image description here

可能发生什么情况?

使用本地 Bitmap 保存图像时, < 坚固> 获得像素方法:

if ((error = AndroidBitmap_getInfo(pEnv, jbitmap, &info)) < 0) {
    LOGE("AndroidBitmap_getInfo() failed! error:%d",error);
}

if (0 == error)
{
    if ((error = AndroidBitmap_lockPixels(pEnv, jbitmap, &pixels)) < 0) {
        LOGE("AndroidBitmap_lockPixels() failed ! error=%d", error);
    }
}

if (0 == error)
{
    if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888)
    {
        LOGI("ANDROID_BITMAP_FORMAT_RGBA_8888");
    }
    else
    {
        LOGI("ANDROID_BITMAP_FORMAT %d",info.format);
    }

    Mat bgra(info.height, info.width, CV_8UC4, pixels);
    Mat image;

    //bgra.copyTo(image);

    // fix pixel order RGBA -> BGRA
    cvtColor(bgra, image, COLOR_RGBA2BGRA);

    vector<int> compression_params;
    compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
    compression_params.push_back(3);

    // save image
    if (!imwrite(filePath, image, compression_params))
    {
        LOGE("saveImage() -> Error saving image!");
        error = -7;
    }

    // release locked pixels
    AndroidBitmap_unlockPixels(pEnv, jbitmap);
}

使用本地 Int 像素数组方法保存图像 < 坚固> :

JNIEXPORT void JNICALL Java_com_vblast_smasher_Smasher_saveImageRaw
    (JNIEnv *pEnv, jobject obj, jstring jFilePath, jintArray jbgra, jint options, jint compression)
{
    jint*  _bgra = pEnv->GetIntArrayElements(jbgra, 0);
    const char *filePath = pEnv->GetStringUTFChars(jFilePath, 0);

    if (NULL != filePath)
    {
        Mat image;
        Mat bgra(outputHeight, outputWidth, CV_8UC4, (unsigned char *)_bgra);

        bgra.copyTo(image);

        if (0 == options)
        {
            // replace existing cache value
            mpCache->insert(filePath, image);
        }

        vector<int> compression_params;
        compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
        compression_params.push_back(compression);

        // save image
        if (!imwrite(filePath, image))
        {
            LOGE("saveImage() -> Error saving image!");
        }
    }

    pEnv->ReleaseIntArrayElements(jbgra, _bgra, 0);
    pEnv->ReleaseStringUTFChars(jFilePath, filePath);
}

Update 05/25/12:
After a little more research I m finding out that this issue does not happen if I get the int array of pixels from the bitmap and pass that directly to the JNI as opposed to what I do currently which is pass the entire Bitmap to the JNI layer then get the pixels and use cvtColor to convert pixels properly. Am I using the right pixel conversion?

最佳回答

在 RGBA 像素中, 有两种方法代表 Alpha, 预多式或非多式。 使用前乘法, R、 G 和 B 值乘以 alpha 的百分比 : 颜色 = (color * alpha) / 255。 这简化了许多混合计算方法, 并经常在图像库中使用。 在保存到不使用 PNG 等前多式字母的格式之前, 颜色值必须是“ 非多式 ” : 颜色 = ( 255 * color) / Alpha 。 如果不是, 颜色会显得太暗; 颜色越透明, 它就越暗。 这看起来像您在这里看到的效果 。

问题回答

在opencv 中,没有所谓的透明图像。 前景和背景图像被适当混合, 以产生透明度的假象 。 请检查 < a href=" http://www. aishack.in/2010/ 07/ 透明性- image-overlays- in- opencv/ > " rel=“ nofollow” > this 查看它是如何完成的 。





相关问题
Python/OpenCV: Converting images taken from capture

I m trying to convert images taken from a capture (webcam) and do some processing on them with OpenCV, but I m having a difficult time.. When trying to convert the image to grayscale, the program ...

How to smooth a histogram?

I want to smooth a histogram. Therefore I tried to smooth the internal matrix of cvHistogram. typedef struct CvHistogram { int type; CvArr* bins; float thresh[CV_MAX_DIM][2]; /* ...

Testing complex datatypes?

What s are some ways of testing complex data types such as video, images, music, etc. I m using TDD and wonder are there alternatives to "gold file" testing for rendering algorithms. I understand that ...

Displaying OpenCV iplimage data structures with wxPython

Here is my current code (language is Python): newFrameImage = cv.QueryFrame(webcam) newFrameImageFile = cv.SaveImage("temp.jpg",newFrameImage) wxImage = wx.Image("temp.jpg", wx.BITMAP_TYPE_ANY)....

What is the memory structure of OpenCV s cvMat?

Imagine I have the following: CvMat* mat = cvCreateMat(3,3,CV_16SC3) This is a 3x3 matrix of integers of channel 3. Now if you look at OpenCV documentation you will find the following as the ...

Face detection and comparison

I m running a small research on face detection and comparison for my article. Currently, I m using rapid face detection based on haar like features based on OpenCV cascade (I ll implement learning ...

热门标签