C++ 更改RGB值
我正在尝试用另一个图像更改图像的一部分 我找不到合并函数 所以我只是想知道,我可以用其他图像改变我想改变的部分的rgb值吗C++ 更改RGB值,c++,c,opencv,image-manipulation,C++,C,Opencv,Image Manipulation,我正在尝试用另一个图像更改图像的一部分 我找不到合并函数 所以我只是想知道,我可以用其他图像改变我想改变的部分的rgb值吗 感谢您的建议您可以尝试以下方式: CvScalar s = cvGet2D(original_cvimage, x, y); // get the (x,y) pixel value cvSet2D(new_cvimage, x, y, s); // set the (x,y) pixel value 您可以尝试以下方法: CvScalar s = cvGet2D(ori
感谢您的建议您可以尝试以下方式:
CvScalar s = cvGet2D(original_cvimage, x, y); // get the (x,y) pixel value
cvSet2D(new_cvimage, x, y, s); // set the (x,y) pixel value
您可以尝试以下方法:
CvScalar s = cvGet2D(original_cvimage, x, y); // get the (x,y) pixel value
cvSet2D(new_cvimage, x, y, s); // set the (x,y) pixel value
如果更改指的是替换,则您可以使用图像ROI(感兴趣区域)功能来直接将原始图像的矩形区域替换为另一图像的矩形区域 假设您的原始图像存储在
A
中,并且希望使用图像B
中的像素更改其中的一部分(矩形区域)
更新:以下是C语言的代码
/**** C ****/
// Acquire Image A and B (here as an example, I'm reading from disk)
IplImage* A = cvLoadImage("image_A.jpg");
IplImage* B = cvLoadImage("image_B.jpg");
// Set the region-of-interest (ROI) for the two images
// such that only the ROI of A and B will be handled
cvSetImageROI(A,cvRect(200,200,128,128));
cvSetImageROI(B,cvRect(0,0,128,128));
// Copy the ROI in B to the ROI in A
cvCopy(B,A);
// Reset the ROI (now the entire image will be handled)
cvResetImageROI(A);
cvResetImageROI(B);
// Display A
cvNamedWindow("Modified A");
cvShowImage("Modified A",A);
cvWaitKey();
// Release the images
cvReleaseImage(&A);
cvReleaseImage(&B);
使用OpenCV 2.0:
// C++ //
// Images A and B have already been loaded .....
// Region in image A starting from (100,100) of width 200 and height 200
Rect RegionA(100,100,200,200);
// Region in image B starting from (50,50) of width 200 and height 200
Rect RegionB(50,50,200,200);
// No copying, just a reference to the ROI of the image
Mat A_ROI(A,RegionA);
Mar B_ROI(B,RegionB);
// Copy all the pixels in RegionB in B to RegionA to A
B.copyTo(A);
如果更改指的是替换,则您可以使用图像ROI(感兴趣区域)功能来直接将原始图像的矩形区域替换为另一图像的矩形区域 假设您的原始图像存储在
A
中,并且希望使用图像B
中的像素更改其中的一部分(矩形区域)
更新:以下是C语言的代码
/**** C ****/
// Acquire Image A and B (here as an example, I'm reading from disk)
IplImage* A = cvLoadImage("image_A.jpg");
IplImage* B = cvLoadImage("image_B.jpg");
// Set the region-of-interest (ROI) for the two images
// such that only the ROI of A and B will be handled
cvSetImageROI(A,cvRect(200,200,128,128));
cvSetImageROI(B,cvRect(0,0,128,128));
// Copy the ROI in B to the ROI in A
cvCopy(B,A);
// Reset the ROI (now the entire image will be handled)
cvResetImageROI(A);
cvResetImageROI(B);
// Display A
cvNamedWindow("Modified A");
cvShowImage("Modified A",A);
cvWaitKey();
// Release the images
cvReleaseImage(&A);
cvReleaseImage(&B);
使用OpenCV 2.0:
// C++ //
// Images A and B have already been loaded .....
// Region in image A starting from (100,100) of width 200 and height 200
Rect RegionA(100,100,200,200);
// Region in image B starting from (50,50) of width 200 and height 200
Rect RegionB(50,50,200,200);
// No copying, just a reference to the ROI of the image
Mat A_ROI(A,RegionA);
Mar B_ROI(B,RegionB);
// Copy all the pixels in RegionB in B to RegionA to A
B.copyTo(A);
我不明白它做什么,你能解释一下吗CVGet2D返回一个代表像素的CvScalar对象。如果您说:s=cvGet2D(original_cvimage,0,0),它将返回像素(0,0)的RGB值。s[0]=蓝色值,s[1]=绿色,s[2]=红色。但那不关你的事。您需要知道的是,在调用s=cvGet2D(original_cvimage,x,y),s将保留原始图像1像素的像素值。通过调用cvSet2D(new_cvimage,x,y,s);您正在将原始图像中的1个像素加载到新图像中。因此,你需要添加一个嵌套循环来遍历图像中的所有像素。这会非常慢。是的,但我读了这篇文章:我的印象是OP不想使用cvSetImageROI和CVCopy。我不明白它做了什么。你能解释一下CVGet2D返回一个代表像素的CvScalar对象吗。如果您说:s=cvGet2D(original_cvimage,0,0),它将返回像素(0,0)的RGB值。s[0]=蓝色值,s[1]=绿色,s[2]=红色。但那不关你的事。您需要知道的是,在调用s=cvGet2D(original_cvimage,x,y),s将保留原始图像1像素的像素值。通过调用cvSet2D(new_cvimage,x,y,s);您正在将原始图像中的1个像素加载到新图像中。所以你需要添加一个嵌套循环来遍历图像中的所有像素。这会非常慢。是的,但是我读到了这个帖子:在这个印象中,OP不想使用CVSETIMEGAREI和CVCONMUMMM,但是你能写一个C代码吗?我不擅长C++。我知道CVSETIMGEARI I把指定的矩形复制到另一个图像,我第一次听到了ROI功能。当我在你的评论下做它的时候,它会做我正在寻找的,嗯,谢谢,但是你能写一个C代码吗?我不擅长C++。我知道CVSETIMGEARI I将指定的矩形复制到另一个图像,我第一次听到了ROI功能。当我理解你在网站附近的评论时,它会满足我的要求