C++使用OpenCV修改位图(bmp)像素点颜色

新手记录备忘,后续业务使用

main():

int main()
{
    std::cout << "Hello World!\n";
    Mat src;
    src = change_special("E:/CodeDemo/2_Study/CppBmpTest/CppBmpTest/test_200_200_0x00ffff.bmp", 20, 20, 0xCD3700);
    if (src.empty()) {
        cout << "could not load image...." << endl;
        return -1;
    }
    namedWindow("output", WINDOW_AUTOSIZE);
    imshow("output", src);
    waitKey(0);
    return 0;
}

方法:

//改变指定行列颜色
Mat change_special(const char* fileName, uint32_t row, uint32_t column, uint32_t color) 
{
    Mat src;
    src = imread(fileName);
    if (src.empty()) {
        cout << "could not load image...." << endl;
        return src;
    }
    Mat dst;
    dst.create(src.size(), src.type());
    int height_color = src.rows;
    int width_color = src.cols;
    int channels = src.channels();
    //这里使用了阵列改色,会隔一行修改,修改60行列
    if (row <= height_color && column <= width_color) {
        uint8_t newr = color >> 16;
        uint8_t newg = color >> 8;
        uint8_t newb = color;
        src.at<Vec3b>(row, column)[0] = newb;//b
        src.at<Vec3b>(row, column)[1] = newg;//g
        src.at<Vec3b>(row, column)[2] = newr;//r
       
        for (int x = row; x <= row+120; x=x+2) {
            for (int y = row; y <= row + 120; y = y + 2) {

                src.at<Vec3b>(x, y)[0] = newb;//b
                src.at<Vec3b>(x, y)[1] = newg;
                src.at<Vec3b>(x, y)[2] = newr;
            }
        }
        
    }
    //保存到指定图片
    imwrite("E:/CodeDemo/2_Study/CppBmpTest/CppBmpTest/save.bmp", src);
    return src;
}

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值