how to get value of pixel from image?
Jun 5, 2016 at 9:37pm UTC
From one tutorial I have taken this code. I added the second block of loops and I have error on line with alpha * (int) p + beta . I cannot find out how to assign the value from image. I tried this: p = *( image.ptr<uchar>(i) );.
The error is error C2440: '=' : cannot convert from 'uchar' to 'uchar *' on line 50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
double alpha; /*< Simple contrast control */
int beta; /*< Simple brightness control */
int main( int argc, char ** argv )
{
cv::Mat image = imread( argv[1] );
if ( image.empty() )
{
std::cout << "image not found" << std::endl;
return -1;
}
if ( image.channels() != 3)
{
std::cout << "not 3 channels in image" << std::endl;
return -2;
}
cv::Mat new_image = Mat::zeros( image.size(), image.type() );
std::cout<<" Basic Linear Transforms " <<std::endl;
std::cout<<"-------------------------" <<std::endl;
std::cout<<"* Enter the alpha value [1.0-3.0]: " ;std::cin>>alpha;
std::cout<<"* Enter the beta value [0-100]: " ; std::cin>>beta;
double t = (double ) getTickCount();
for ( int y = 0; y < image.rows; y++ ) {
for ( int x = 0; x < image.cols; x++ ) {
for ( int c = 0; c < 3; c++ ) {
new_image.at<Vec3b>(y,x)[c] =
saturate_cast<uchar>(
alpha*( image.at<Vec3b>(y,x)[c] ) + beta
);
}
}
}
t = (getTickCount()-t)/getTickFrequency();
std::cout << ".at() time:" << t << "ms" << std::endl;
/** 0.45ms **/
char p;
t = (double ) getTickCount();
for ( int y = 0; y < image.rows; y++ ) {
for ( int x = 0; x < image.cols; x++ ) {
for ( int c = 0; c < 3; c++ ) {
int i = y*x+c;
p = *( image.ptr<uchar>(i) );
new_image.ptr<uchar>(i) =
saturate_cast<uchar>(
alpha * (int ) p + beta
);
}
}
}
t = (getTickCount()-t)/getTickFrequency();
std::cout << ".at() time:" << t << "ms" << std::endl;
namedWindow("Original Image" , 1);
namedWindow("New Image" , 1);
imshow("Original Image" , image);
imshow("New Image" , new_image);
waitKey();
return 0;
}
Last edited on Jun 5, 2016 at 9:38pm UTC
Topic archived. No new replies allowed.