Reputation: 2057
I am totally new in openCV and stuck at one point.
I have a grey scale image but I need to normalize this image. For example: I have a cv::mat image which may have a matrix containing some values and each index. As I have gray image it may contain only 1 value per index. Now I need to divide each value by 255.
Is there any method or facility available in openCV in C++? For this scenario, I believe the method I want to use is called normalization in openCV?
cv::Mat originalMat = [OSInference cvMatFromUIImage:imgBeforeProccessing];
cv::Mat img2;
cv::cvtColor(originalMat, img2,CV_BGR2GRAY);
cv::resize(img2, img2, cv::Size(128, 128), 0, 0, CV_INTER_CUBIC);
cv::Mat img3;
Now how to normalise (this means to divide each value in the matrix by 255)??
I am converting the mat image to an iOS image as follows:
+ (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat{
NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
CGColorSpaceRef colorspace;
if (cvMat.elemSize() == 1) {
colorspace = CGColorSpaceCreateDeviceGray();
}else{
colorspace = CGColorSpaceCreateDeviceRGB();
}
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
// Create CGImage from cv::Mat
CGImageRef imageRef = CGImageCreate(cvMat.cols, cvMat.rows, 8, 8 * cvMat.elemSize(), cvMat.step[0], colorspace, kCGImageAlphaNone | kCGBitmapByteOrderDefault, provider, NULL, false, kCGRenderingIntentDefault);
// get uiimage from cgimage
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorspace);
return finalImage;
}
Upvotes: 3
Views: 33147
Reputation: 321
To normalize cv::Mat you can use cv::normalize. I write some code to help you.
uchar data[] = {0, 63, 127, 255};
cv::Mat im(2, 2, CV_8UC1, data), output;
# this what you need 0 -> min value after norm
# 1 -> max value after nom
# cv::NORM_MINMAX normalize for min and max values
cv::normalize(im, output, 0, 1, cv::NORM_MINMAX);
std::cout
<< im << '\n'
<< output << '\n';
#==================================================
# output
[ 0, 63;
127, 255]
[ 0, 0;
0, 1]
Upvotes: 7