Ask Your Question
0

Getting Blackscreen when i run my program

asked 2014-04-24 07:10:01 -0600

FLY gravatar image

updated 2014-04-25 01:29:38 -0600

I am running the code but whenever i run it give me the black screen as output rather than some good results which i want

cv::Mat img1 = cv::imread("E:\\raw_3.jpg");
cv::Mat img2 = cv::Mat (img1.size(),img1.type());
std::vector<cv::Mat> colors_1;
cv::split(img2, colors_1);
colors_1[0] = 173;
colors_1[1] = 221;
colors_1[2] = 246;
cv::merge(colors_1,img2);
Mat result(img1.size(), CV_8UC3);

for(int i = 0; i < img1.size().height; ++i){
    for(int j = 0; j < img1.size().width; ++j){
        for (int c=0 ; c<img1.channels();c++){
            uchar target = img1.at<uchar>(i, 3*j+c)/255. ;
            uchar blend = img2.at<uchar>(i, 3*j+c)/255. ;
            if(target > 0.5){
                result.at<uchar>(i, 3*j+c) = cv::saturate_cast<uchar>((1 - (1-target) * (1-2*(blend-0.5))))/255.;
                }
            else{
                result.at<uchar>(i, 3*j+c) = cv::saturate_cast<uchar>(target * (2*blend))/255.;
                }
        }
    }
edit retag flag offensive close merge delete

Comments

You are getting black window because cv::saturate_cast&lt;uchar&gt;((1 - (1-target) * (1-2*(blend-0.5))))/255.; and cv::saturate_cast&lt;uchar&gt;(target * (2*blend))/255.; might be giving zero , just print value of those and confirm it.

Haris gravatar imageHaris ( 2014-04-25 01:55:30 -0600 )edit

Yes its range is from 0-1 but what would be the solution i tried it with float too

FLY gravatar imageFLY ( 2014-04-25 01:59:28 -0600 )edit

i also tried it using ((1 - (1-target) * (1-2*(blend-0.5))))*255.

FLY gravatar imageFLY ( 2014-04-25 02:22:53 -0600 )edit

2 answers

Sort by » oldest newest most voted
3

answered 2014-04-25 04:59:49 -0600

FLY gravatar image

updated 2014-04-25 05:51:43 -0600

Getting a good idea from @Haris Solution , I did the changes and below code works for me

Mat result(img1.size(), CV_32FC3);
for(int i = 0; i < img1.size().height; ++i){
    for(int j = 0; j < img1.size().width; ++j){

        for (int c=0 ; c<img1.channels();c++){
            float target = (float)img1.at<uchar>(i, 3*j+c)/255.0 ;
            float blend =  (float)img2.at<uchar>(i, 3*j+c)/255.0 ;
            if(target > 0.5){
                result.at<float>(i, 3*j+c) = ((1 - (1-target) * (1-2*(blend-0.5))));

                }
            else{
                result.at<float>(i, 3*j+c) = (target * (2*blend));
                }
        }
    }
}

When we already divide target and blend by 255 we do not need to divide them again in the result and then convert it by

result.convertTo(result,CV_8UC3,255);

and we will get the result

edit flag offensive delete link more
1

answered 2014-04-25 02:46:41 -0600

Haris gravatar image

updated 2014-04-25 02:47:38 -0600

You need to use a floating point Mat to store float data,

So change your code to

Mat result(img1.size(), CV_32FC3);
for(int i = 0; i < img1.size().height; ++i){
    for(int j = 0; j < img1.size().width; ++j){

        for (int c=0 ; c<img1.channels();c++){
            float target = (float)img1.at<uchar>(i, 3*j+c)/255.0 ;
            float blend =  (float)img2.at<uchar>(i, 3*j+c)/255.0 ;
            if(target > 0.5){
                result.at<float>(i, 3*j+c) = cv::saturate_cast<uchar>((1 - (1-target) * (1-2*(blend-0.5))))/255.;

                }
            else{
                result.at<float>(i, 3*j+c) = cv::saturate_cast<uchar>(target * (2*blend))/255;
                }
        }
    }
}

And for displaying just convert it to 8 bit,

double min;
double max;
cv::minMaxIdx(result, &min, &max);
cv::Mat final;

result.convertTo(final,CV_8UC1, 255 / (max-min), -min);
imshow("final",final);
edit flag offensive delete link more

Comments

Its giving the strange result not according to the formula

FLY gravatar imageFLY ( 2014-04-25 02:51:17 -0600 )edit

cv::saturate_cast<uchar>(target * (2*blend))/255; // this will lead to [0,1] binarization, most of them 0

should be:

cv::saturate_cast<uchar>(target * (2*blend)/255); // divide, while it's float, then saturate

(same for the other saturate_cast above)

your own solution cleverly avoids it ;)

berak gravatar imageberak ( 2014-04-25 05:13:44 -0600 )edit

@berak if we did the division above on rows and columns then why we need here and berak your solution give the image complete white , i tried it and yes sorry i didnt use saturate_cast in my solution , sorry again

FLY gravatar imageFLY ( 2014-04-25 05:48:27 -0600 )edit

touché .;)

berak gravatar imageberak ( 2014-04-25 06:49:08 -0600 )edit

Question Tools

Stats

Asked: 2014-04-24 07:10:01 -0600

Seen: 357 times

Last updated: Apr 29 '14