Ask Your Question
1

negative value of cv::Mat

asked 2018-03-27 03:15:49 -0600

Weifa Gan gravatar image

updated 2018-03-27 03:19:46 -0600

LBerger gravatar image

I observe that when the Mat data,which were loaded from a picture, transform negative value to zero after subtracting by a number.But I don't need this transform,what should i do ?pleace. The codes are as follows.

#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{

        Mat M;
    Point2i p;
    M = imread("D:\\Documents\\Desktop\\Face data\\AR1\\001\\AR001-1.tif", 1);
    cvtColor(M, M, CV_BGR2GRAY);
    M = M - 300;
    cout << M << endl;

      waitKey(0);
    system("pause");
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-03-27 03:21:19 -0600

berak gravatar image

updated 2018-03-27 03:24:42 -0600

so, the type is uchar. there cannot be negative numbers.

(one could either overflow or saturate, opencv's arithmetic ops do saturation here.)

if you wanted negative numbers here, you have to convert to a type, which allows it, like CV_32S:

M.convertTo(M, CV_32S);
edit flag offensive delete link more

Comments

2

Thank you very much!

Weifa Gan gravatar imageWeifa Gan ( 2018-03-27 03:52:43 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-03-27 03:15:49 -0600

Seen: 3,752 times

Last updated: Mar 27 '18