【问题标题】:Changing RGB values更改 RGB 值
【发布时间】:2010-12-07 22:40:37
【问题描述】:

我正在尝试用另一个图像更改图像的一部分 我找不到合并功能 所以我只是发现我可以改变我想用其他图像改变的部分的 rgb 值 RGB 值是否可能

感谢您的建议

【问题讨论】:

    标签: c++ c opencv image-manipulation


    【解决方案1】:

    如果change你的意思是replace,那么你可以使用图像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);
    

    【讨论】:

    • hmmm 谢谢,但你能写一个 C 代码吗我不擅长 C++ 我了解到 cvSetImageROI 将指定的矩形复制到另一个图像我第一次听到 ROI 函数本身。正如我所了解的那样,您在附近的 cmets 会做我正在寻找的事情
    【解决方案2】:

    你可以试试这样的:

    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
    

    【讨论】:

    • 我不明白它是做什么的你能解释一下
    • 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 个像素。所以你需要添加一个嵌套循环来遍历图像中的所有像素。
    • 这会非常慢
    • 是的,但我读了这个帖子:stackoverflow.com/questions/1571683/opencv-image-on-image 并认为 OP 不想使用 cvSetImageROI 和 cvCopy
    猜你喜欢
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 2017-06-28
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    相关资源
    最近更新 更多