通过cmake+opencv更改图片颜色及抠图(ubuntu环境)

一、更改图片颜色

1.CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(pic_make VERSION 1.0.0)
add_executable(pic_make pic_make.cpp)
find_package(OpenCV REQUIRED)
target_link_libraries(pic_make ${OpenCV_LIBS})

2.cmake . -B build

3.pic_make.cpp

#include 
using namespace cv;
#include 
using namespace std;

int main(int argc, char const *argv[])
{
    Mat srcImg = imread("../china.png", IMREAD_UNCHANGED);
    Mat srcImg1 = imread("../china.png", 0);
    Mat srcImg2 = imread("../china.png", 1);
    
    // 颜色有空间 RGB(1) RGBA(-1) 透明度 灰色空间(0)

    //存储阶段的颜色值
    vector colors;
    unsigned long index = 0;

    for (int i = 0; i < 6; i++)
    {
        for (int j = 0; j < 6; j++)
        {
            for (int k = 0; k < 6; k++)
            {
                colors.push_back(Vec3d());
                colors[index][0] = i / 5.0 * 255;
                colors[index][1] = j / 5.0 * 255;
                colors[index][2] = k / 5.0 * 255;
            }
            
        }
       
    }

    // 更改对应的颜色值
    index = 0;
    Mat temp = srcImg.clone();
    while (index < colors.size())
    {
        for (int r = 0; r < srcImg.rows; r++)
        {
            for (int c = 0; c < srcImg.cols; c++)
            {
                //获取像素点的颜色值
                //Vec4b &pixel = srcImg.at(r, c);
                Vec4b &pixel_temp = temp.at(r, c);
                //如果当前点的透明度为0
                if (pixel_temp[3] == 0)
                {
                    continue;
                }
                else
                {
                    for (int i = 0; i < 3; i++)
                    {
                        pixel_temp[i] = colors[index][i];
                    }
                    
                }
                
            }
            
        }
        
        char outImagePath[64] = {};
        sprintf(outImagePath, "../out_img/out_img_%.0f_%.0f_%.0f.png", colors[index][0], colors[index][1], colors[index][2]);
        imwrite(outImagePath, temp);
        index++;
    }
    


    cout << srcImg.channels() << endl;
    cout << srcImg1.channels() << endl;
    cout << srcImg2.channels() << endl;

    imshow("-1", srcImg);
    imshow("0", srcImg1);
    imshow("1", srcImg2);



    waitKey(0);
    
    return 0;
}

4.cd build

  make

  ./pic_make


二、抠图

1.CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(GrabCut VERSION 1.0.0)
add_executable(GrabCut GrabCut.cpp)
find_package(OpenCV REQUIRED)
target_link_libraries(GrabCut ${OpenCV_LIBS})

2.cmake . -B build

3.GrabCut.cpp

#include 
#include 
using namespace std;
using namespace cv;

int main(int argc, char const *argv[])
{
    Mat srcImg = imread("../leifengtower.jpg");
    Mat mask, bgModel, fgModel;
    Rect rect(Point(1, 1), Point(srcImg.cols, srcImg.rows));
    grabCut(srcImg, mask, rect, bgModel, fgModel, 20, GC_INIT_WITH_RECT);
    rectangle(srcImg, rect, Scalar(255), 2, 8);
  
    imshow("src", srcImg);
    imshow("mask", (mask & 1) * 255);
    //imshow("bgModel", bgModel);
    //imshow("fgModel", fgModel);
    Mat resultMask = (mask & 1) * 255;

    //Mat resultImg = Mat(srcImg.rows, srcImg.cols, CV_8UC3);
    Mat resultImg = imread("../sky.jpg");
    resize(resultImg, resultImg, Size(srcImg.cols, srcImg.rows));

    // for (int i = 0; i < srcImg.rows; i++)
    // {
    //     for (int j = 0; j < srcImg.cols; j++)
    //     {
    //         Vec3b &pixel_src = srcImg.at(i, j);
    //         Vec3b &pixel_result = resultImg.at(i, j);
    //         uchar &pixel_mask = resultMask.at(i, j);
    //         if (pixel_mask == 255)
    //         {
    //             for (int k = 0; k < 3; k++)
    //             {
    //                 pixel_result[k] = pixel_src[k];
    //             }
                
    //         }
    //     }
        
    // }

    srcImg.copyTo(resultImg, resultMask);

    imshow("result", resultImg);
    waitKey(0);
    return 0;
}

4.cd build

  make

  ./GrabCut

展示结果:

通过cmake+opencv更改图片颜色及抠图(ubuntu环境)_第1张图片

你可能感兴趣的:(opencv,c++,vscode,ubuntu,图像处理)