Ask Your Question
0

subtract method replace negative values with zeros

asked 2018-07-03 06:32:43 -0600

Sanaullah gravatar image

updated 2018-07-03 07:52:18 -0600

berak gravatar image

I am doing subtraction of grayscale images. i want to keep negative values. but subtract method in opencv replace negative values with zeros. User Mat types are CV_32S, CV_32F.

public double corr2(Mat img1, Mat img2){

        Mat grayImg1,grayImg2;
        grayImg1=new Mat(300,300,CV_32S);
        grayImg2=new Mat(300,300,CV_32S);
        Imgproc.cvtColor(img1,grayImg1,COLOR_RGB2GRAY);
        Imgproc.cvtColor(img2,grayImg2,COLOR_RGB2GRAY);
        Scalar img1mean=Core.mean(grayImg1);
        Scalar img2mean=Core.mean(grayImg2);
        Core.subtract(grayImg1,img1mean,grayImg1);
edit retag flag offensive close merge delete

Comments

what is the input type ? (of img1 and img2) ?

berak gravatar imageberak ( 2018-07-03 07:28:17 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-07-03 07:51:57 -0600

berak gravatar image

Core.subtract will indeed saturate results for unsigned data types. note, that your allocation here:

   grayImg1=new Mat(300,300,CV_32S);

will get overwritten (it's useless, and you only fool yourself here), and the grayscale result has the same depth as the input (CV_8U);

also note, that opencv uses BGR pixel order, so you have to use COLOR_BGR2GRAY.

to preserve thnegative values, you have to convert the image to some signed (and larger) type, so all in all it should be:

    Mat grayImg1 = new Mat();
    Imgproc.cvtColor(img1, grayImg1, Imgproc.COLOR_BGR2GRAY);

    Mat gray_32 = new Mat();
    grayImg1.convertTo(gray_32, CvType.CV_32S);

    Scalar img1mean = Core.mean(grayImg1);
    Core.subtract(gray_32, img1mean, gray_32);
edit flag offensive delete link more

Comments

thanks. it works

Sanaullah gravatar imageSanaullah ( 2018-07-03 22:23:49 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-07-03 06:31:52 -0600

Seen: 1,231 times

Last updated: Jul 03 '18