John
John

Reputation: 3070

How to set zeros values in a part of cv::Mat

I have a cv::Mat of a RGB image as

cv::Mat cv_img 

I want to set zeros value for cv_img at some positions. For example from the bottom to the half location of the image will be filled by zero values. How can I do it in c++ and opencv? Thanks all.

I have searched a setTo function and mask may be a candidate solution, but how to define a binary mask is difficult for me.

cv_img.setTo(Scalar(0,0,0), mask);

Upvotes: 0

Views: 1247

Answers (2)

user4016525
user4016525

Reputation: 29

You can try this:

int w = cv_img.cols;
int h = cv_img.rows; 
cv::Rect rectZero(0, h/2, w, h/2);
cv_img(rectZero) = cv::Scalar(0,0,0);

Upvotes: 1

Yunus Temurlenk
Yunus Temurlenk

Reputation: 4342

You can achieve it by setting the pixels with a desired value. Just define the intervals of roi(region of interest.

Here is a simple code to guide:

#include <opencv2/opencv.hpp>

using namespace cv;

int main()
{
    Mat img = imread("/ur/img/dir/img.jpg");

    for(int i=img.rows/2; i<img.rows;i++)
    {
        for(int j=0; j<img.cols; j++)
        {
            img.at<Vec3b>(Point(j,i))[0] = 0;
            img.at<Vec3b>(Point(j,i))[1] = 0;
            img.at<Vec3b>(Point(j,i))[2] = 0;

        }
    }

    imshow("Result",img);
    waitKey(0);
                
    return 0;
}

Upvotes: 1

Related Questions