user3923119
user3923119

Reputation:

modify image in c++ using opencv

i want to modify image in c++ using opencv and this is my code

 #include <cv.h>
 #include <highgui.h>  
 using namespace std;
 int main(int* argc ,char* argv[]){  
 IplImage* image =cvLoadImage ("D://s1//1.pgm",0);
 //cvShowImage( "Source",image);
 int h=image->height;
 int w=image->width;
 CvScalar pix;
 IplImage* img2 = cvCreateImage( cvSize(h,w), 8, 1 );
 CvMat* mat1  = cvCreateMat(h,w,CV_32FC1);
 CvMat* mat2  = cvCreateMat(h,w,CV_32FC1);
 cvConvert(image,mat1);
 for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
    if(i==j){
    pix =1;
    cvSet2D(mat2,i,j,pix.val[0]);}
    else{
    pix = cvGet2D( mat1,i,j);
    cvSet2D(mat2,i,j,pix.val[0]); }  
    }      
 }
 cvConvert(mat2,img2 );
 cvShowImage( "image",img2);
 cvWaitKey(0);
 return 0;}

but it not worked,I need your help and your advice,please Join us your perspectives.

Upvotes: 0

Views: 263

Answers (2)

user3923119
user3923119

Reputation:

 #include <cv.h>
 #include <highgui.h>  
 using namespace std;
 int main(int argc ,char** argv[]){  
 CvScalar pix;
 IplImage* img2 = cvCreateImage( cvSize(500,500), 32,3 );
 for(int i=0;i<500;i++)
    for(int j=0;j<500;j++){
    pix = cvGet2D( img2,i,j);
    pix.val[0]=rand()%150;
    pix.val[1]=rand()%150;
    pix.val[2]=rand()%150;
    cvSet2D(img2,i,j,pix); }     
 cvShowImage( "image",img2);
 cvWaitKey(0);
 return 0;}

Upvotes: 1

The line

cvSet2D(mat2,i,j,pix.val[0]);

is syntactically incorrect. It should be

cvSet2D(mat2,i,j,pix);

If you want to modify the RGB values of pix, that has to be done before this line.

for an arbitrary example,

pix.val[0]=pix.val[0]/2;

If you tell us what exactly the modification is, the question will be easier to answer.

Upvotes: 0

Related Questions