图片01化

Posted _less is more

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片01化相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <opencv2/opencv.hpp>
#include <unordered_set>
using namespace std;

int main() 

    std::string path = "C:/Users/ASUS/Pictures/Image/rick_canny.png";
    cv::Mat img = cv::imread(path);

    int height = img.rows;
    int width = img.cols;

    int cellHeight = 1, cellWidth = 1;
    cv::Mat img2 = img.clone();
    cv::resize(img, img2, cv::Size((width / cellWidth), (height / cellHeight)), 0.0, 0.0, cv::INTER_NEAREST);

    int newWidth = img.cols;
    int newHeight = img.rows;

    cv::Mat newImg = cv::Mat::zeros(height, width, CV_8UC3);
    unordered_set<string> s;
    string num = "0";
    for (int i = 0; i < img2.cols; i+=1) 
        for (int j = 0; j < img2.rows; j+=1) 
            cv::Scalar color = img2.at<cv::Vec3b>(j, i);
            int b = color[0];
            int g = color[1];
            int r = color[2];
            int k = (b + g + r) / 3;
            int newi = i * cellWidth;
            int newj = j * cellHeight;
            if (k > 128)
                int flag = 1;
                for (int m = newi - 8; m < newi + 9; m++) 
                    for (int n = newj - 8; n < newj + 9; n++) 
                        string hash = to_string(m) + " " + to_string(n);
                        if (s.count(hash) != 0) 
                            flag = 0;
                            break;
                        
                    
                    if (!flag) break;
                
                if (flag) 
                    cv::putText(newImg, num, cv::Point(newi, newj), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.5, cv::Scalar(0, g, 0));
                    s.insert(to_string(newi) + " " + to_string(newj));
                    if (num == "0") num = "1";
                    else num = "0";
                
            
        
    

    cv::addWeighted(img, 0.2, newImg, 0.8, 0, img);
    cv::imwrite("C:/Users/ASUS/Pictures/Image/rick_01.png", newImg);
    cv::imshow("def", newImg);
    cv::waitKey(0);



效果(左绿)

以上是关于图片01化的主要内容,如果未能解决你的问题,请参考以下文章

使用Xmanager 图形化连接Linux

c#中实现图片灰度化技术详解

Ubuntu 14.04 下使用 OpenCV 图片二值化处理

图片灰度化

Python中具有抗锯齿的光栅化算法

将 WriteableBitmap 线程化到图像控件