Note that there are some explanatory texts on larger screens.

plurals
  1. POImage transparency darkened when saved using OpenCv
    primarykey
    data
    text
    <p>I created a drawing application where I allow the user to draw and save the image to later reload to continue drawing. Essentially, I'm passing the drawing as a bitmap to the JNI layer to be saved and the same to load a previous drawing.</p> <p>I'm using OpenCv to write and read to png file.</p> <p>I'm noticing something weird in terms of the transparencies of the image. It almost seems as the transparency is being calculated against a black color on OpenCv? Take a look a the images attached, the contain lines that have transparencies.</p> <p><strong>Correct transparency by passing int array to native code, no color conversion needed:</strong> <img src="https://i.stack.imgur.com/pulHh.jpg" alt="enter image description here"></p> <p><strong>Darkened transparency by passing Bitmap object to native code, color conversion needed:</strong> <img src="https://i.stack.imgur.com/MsfuE.jpg" alt="enter image description here"></p> <p>What could potentially be happening?</p> <p><strong>Saving image using native Bitmap get pixel methods:</strong> </p> <pre><code>if ((error = AndroidBitmap_getInfo(pEnv, jbitmap, &amp;info)) &lt; 0) { LOGE("AndroidBitmap_getInfo() failed! error:%d",error); } if (0 == error) { if ((error = AndroidBitmap_lockPixels(pEnv, jbitmap, &amp;pixels)) &lt; 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 -&gt; BGRA cvtColor(bgra, image, COLOR_RGBA2BGRA); vector&lt;int&gt; 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() -&gt; Error saving image!"); error = -7; } // release locked pixels AndroidBitmap_unlockPixels(pEnv, jbitmap); } </code></pre> <p><strong>Saving image using native int pixel array methods:</strong> </p> <pre><code>JNIEXPORT void JNICALL Java_com_vblast_smasher_Smasher_saveImageRaw (JNIEnv *pEnv, jobject obj, jstring jFilePath, jintArray jbgra, jint options, jint compression) { jint* _bgra = pEnv-&gt;GetIntArrayElements(jbgra, 0); const char *filePath = pEnv-&gt;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-&gt;insert(filePath, image); } vector&lt;int&gt; compression_params; compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION); compression_params.push_back(compression); // save image if (!imwrite(filePath, image)) { LOGE("saveImage() -&gt; Error saving image!"); } } pEnv-&gt;ReleaseIntArrayElements(jbgra, _bgra, 0); pEnv-&gt;ReleaseStringUTFChars(jFilePath, filePath); } </code></pre> <p><strong>Update 05/25/12:</strong><br> 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 <code>cvtColor</code> to convert pixels properly. Am I using the right pixel conversion?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload