image shows result in VS2010 but not showing output in Android using OpenCV -


my code works in visual studio using opencv. not showing output in android ndk application, , not showing error

below code.

void sow(mat& img1, mat& img2, mat& out) {     mat result(img1.size(), cv_32fc4);                                 img2 = mat(img1.size(), img1.type(), scalar(186, 44, 28, 0));      (int = 0; < img1.size().height; ++i){         (int j = 0; j < img1.size().width; ++j){              (int c = 0; c<img1.channels(); c++){                // iterate through colors                  //formula                  float target = (float)img1.at<uchar>(i, 4 * j + c) / 255.;                 float blend = (float)img2.at<uchar>(i, 4 * j + c) / 255.;                 if (blend > 0.5){                     result.at<float>(i, 4 * j + c) = ((1 - (1 - target) * (1 - 2 * (blend - 0.5))));                 }                 else{                     result.at<float>(i, 4 * j + c) = (target * (2 * blend));                 }             }         }     }     result.convertto(out, cv_8uc4, 255);  } 

it works me in visual studio 8uc3 image, not 8uc4 image in android. other n output in imageview, there no error.

the same thing works me in android when change formula, formula works me on visual studio . images work me when have same code different formula's in

if (blend > 0.5) {     result.at<float>(i, 4 * j + c) = ((1 - (1 - target) * (1 - 2 * (blend - 0.5)))); } else {     result.at<float>(i, 4 * j + c) = (target * (2 * blend)); } 

and showing output.

in same way when use :

void bl(mat& img1, mat& img2, mat& out) {     img2 = mat(img1.size(), img1.type(), scalar(186, 44, 28, 0));     max(img1, img2, out); } 

it shows output when use max(img1, img2, out); in same code not showing output. , working in visual studio not in android using ndk. tested jni , work correctly, java code correct too.

input image : enter image description here

resultant image after result :

enter image description here

part 1

i think problem setting alpha 0 (transparent) in line:

img2 = mat(img1.size(), img1.type(), scalar(186, 44, 28, 0)); 

change to

img2 = mat(img1.size(), img1.type(), scalar(186, 44, 28, 255)); 

since difference alpha channel, first place look. if either img1 or img2 has 0 alpha result transparent! suggested way handle this, according this question, result.alpha = 1 - (1 - target.alpha) * (1 - blend.alpha)

try this:

 void sow(mat& img1, mat& img2, mat& out) {     mat result(img1.size(), cv_32fc4);                                 img2 = mat(img1.size(), img1.type(), scalar(186, 44, 28, 255));      (int = 0; < img1.rows; ++i)     {         vec4b* i1 = img1.ptr<vec4b>(i);         vec4b* i2 = img2.ptr<vec4b>(i);         vec4f* r  = result.ptr<vec4f>(i);          (int j = 0; j < img1.cols; ++j)         {             vec4b p1 = i1[j];             vec4b p2 = i2[j];             vec4f& pr = r[j];              // blend overlay color channels             (int c = 0; c < 3; ++c)             {                  //formula                  float target = (float) p1[c] / 255.0f;                 float blend = (float) p2[c] / 255.0f;                 if (blend > 0.5)                 {                     pr[c] = 1 - (1 - target) * (1 - 2 * (blend - 0.5f));                 }                 else                 {                     pr[c] = target * 2 * blend;                 }             }             // alpha channel             float target_alpha  = (float) p1[3] / 255.0f;             float blend_alpha = (float) p2[3] / 255.0f;             pr[3] = 1 - (1 - target_alpha) * (1 - blend_alpha);         }     }     result.convertto(out, cv_8uc4, 255); } 

with input image generates desired result:

enter image description here


also, problem might jni code, check code calling sow() works correctly, return img1 & img2:

void sow(mat& img1, mat& img2, mat& out) {     img1.copyto(out); } 

btw, except img2 call reference on mats superfluous. e.g.

void sow(mat img1, mat& img2, mat out) {     img2 = mat(img1.size(), img1.type(), scalar(186, 44, 28, 255));     img2.copyto(out); } 

part 2 reason

void bl(mat& img1, mat& img2, mat& out) {     img2 = mat(img1.size(), img1.type(), scalar(186, 44, 28, 0));     min(img1, img2, out); } 

doesn't display alpha channel in out set 0 (completely transparent). change alpha 255 in img2 , should work:

void bl(mat& img1, mat& img2, mat& out) {     img2 = mat(img1.size(), img1.type(), scalar(186, 44, 28, 255));     min(img1, img2, out); } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -