2009-10-19 70 views
0

我想改變一個圖像的一部分與另一個圖像 我找不到合併功能 所以我只是發生,我可以改變我想改變其他部分的RGB值圖像RGB值是有可能改變RGB值

感謝建議

回答

3

如果變化你的意思取代,那麼你可以使用圖像ROI(感興趣區域)功能直接用原始圖像的矩形區域替換您的原始圖像的矩形區域非常有效

假設您的原始圖像存儲在A中,並且想要使用圖像中的像素B更改它的一部分(矩形區域)。

UPDATE:這裏在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); 
+0

嗯,謝謝,但你能寫一個C代碼我不擅長C++ 我知道cvSetImageROI複製指定的矩形到另一個圖像我第一次聽到ROI函數本身。正如我承諾和你的意見,它將只是做我所尋找的 – eomer 2009-10-20 06:10:29

+0

好吧,C代碼是 – Jacob 2009-10-20 14:44:19

+0

謝謝雅各布工程完美 – eomer 2009-10-21 09:09:32

0

你可以嘗試這樣的事:

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 
+0

我不明白這是什麼做的 你能解釋 – eomer 2009-10-19 09:47:00

+0

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個像素。所以你需要添加一個嵌套循環來遍歷圖像中的所有像素。 – anyone 2009-10-19 10:48:53

+0

這將**非常緩慢**。 – Jacob 2009-10-19 14:13:44