OpenCV——黑白调整

参考算法:

闲人阿发伯的博客


// define head function
#ifndef PS_ALGORITHM_H_INCLUDED
#define PS_ALGORITHM_H_INCLUDED

#include <iostream>
#include <string>
#include "cv.h"
#include "highgui.h"
#include "cxmat.hpp"
#include "cxcore.hpp"

using namespace std;
using namespace cv;

void Show_Image(Mat&, const string &);

#endif // PS_ALGORITHM_H_INCLUDED

/*
This program will transform
the color image to Black-whithe
image.

*/

#include "PS_Algorithm.h"
#include <time.h>

using namespace std;
using namespace cv;

int main(void)
{
    string Img_name("9.jpg");
    Mat Img_in;
    Img_in=imread(Img_name);
    Show_Image(Img_in, Img_name);

    Mat Img_out(Img_in.size(), CV_32FC3);
    Img_in.convertTo(Img_out, CV_32FC3);

    Mat R(Img_in.size(),CV_32FC1);
    Mat G(Img_in.size(),CV_32FC1);
    Mat B(Img_in.size(),CV_32FC1);

    Mat I(Img_in.size(),CV_32FC1);

    Mat BW_out(Img_in.size(), CV_32FC1);

    Mat rgb[]={B, G, R};
    cv::split(Img_out, rgb);

    I=B+G+R;

    float maxVal, minVal, midVal;

    float color_ratio[6]={0.4,0.6,0.4,0.6,0.2,0.8};
    float r_max_mid, r_max;
    int Ind;

    for(int i=0; i<I.rows; i++)
    {
        for(int j=0; j<I.cols; j++)
        {
            maxVal=std::max(R.at<float>(i,j), std::max(G.at<float>(i,j),
                                                       B.at<float>(i,j)));
            minVal=std::min(R.at<float>(i,j), std::min(G.at<float>(i,j),
                                                       B.at<float>(i,j)));
            midVal=I.at<float>(i,j)-maxVal-minVal;

            if(minVal==R.at<float>(i,j))
            {
                Ind=0;
            }
            else if(minVal==G.at<float>(i,j))
            {
                Ind=2;
            }
            else
            {
                Ind=4;
            }
            r_max_mid=color_ratio[(Ind+3)%6+1];

            if(maxVal==R.at<float>(i,j))
            {
                Ind=1;
            }
            else if(maxVal==G.at<float>(i,j))
            {
                Ind=3;
            }
            else
            {
                Ind=5;
            }

            r_max=color_ratio[Ind];

            BW_out.at<float>(i,j)=(maxVal-midVal)*r_max+(midVal-minVal)
                      *r_max_mid+minVal;

        }
    }

    BW_out=BW_out/255;
    Show_Image(BW_out, "out");

    imwrite("out.jpg", BW_out*255);

    waitKey();
    cout<<"All is well."<<endl;

}


// define the show image
#include "PS_Algorithm.h"
#include <iostream>
#include <string>

using namespace std;
using namespace cv;

void Show_Image(Mat& Image, const string& str)
{
    namedWindow(str.c_str(),CV_WINDOW_AUTOSIZE);
    imshow(str.c_str(), Image);

}


原图 


效果图


转载于:https://www.cnblogs.com/mtcnn/p/9412600.html

### 如何使用 OpenCV 检测图像中的黑色和白色区域 为了检测图像中的黑色和白色区域,可以利用 OpenCV 提供的二值化功能以及轮廓检测技术。以下是具体方法: #### 图像预处理 在开始之前,需要加载目标图像并将它转换为灰度图以便简化计算过程[^1]。 ```python import cv2 import numpy as np # 加载原始图像 image = cv2.imread('input_image.jpg') # 转换为灰度图 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ``` #### 黑色区域检测 通过设置合适的阈值来分离黑色像素和其他颜色像素。通常情况下,较低的亮度值表示更接近于黑色的颜色[^3]。 ```python # 定义用于提取黑色区域的阈值参数 black_threshold_value = 50 # 可根据实际需求调整此数值 _, black_mask = cv2.threshold(gray, black_threshold_value, 255, cv2.THRESH_BINARY_INV) # 显示结果 cv2.imshow("Black Mask", black_mask) cv2.waitKey(0) ``` 上述代码片段中 `cv2.THRESH_BINARY_INV` 参数的作用是对输入图像执行反向二值化操作——低于指定阈值的部分被标记为白色(即保留),其余部分则变为黑色。 #### 白色区域检测 对于白色的识别,则需采用相反策略:设定较高的阈值得到仅包含高亮像素的结果集[^2]。 ```python white_threshold_value = 200 # 同样可以根据具体情况修改该值 _, white_mask = cv2.threshold(gray, white_threshold_value, 255, cv2.THRESH_BINARY) # 展示最终效果 cv2.imshow("White Mask", white_mask) cv2.destroyAllWindows() ``` 这里采用了正向二值化的模式 (`cv2.THRESH_BINARY`) 来突出显示高于给定界限的所有数据点作为前景对象。 #### 结果可视化与分析 一旦获取到了分别代表黑、白两色域的数据掩模之后,就可以进一步运用形态学运算或者寻找连通组件等方式来进行更加细致的空间分布研究[^4]。 例如可以通过绘制边界框的方式直观呈现所发现的关键部位位置信息: ```python contours_black, _ = cv2.findContours(black_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours_white, _ = cv2.findContours(white_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) result_img = image.copy() for cnt in contours_black: x,y,w,h = cv2.boundingRect(cnt) cv2.rectangle(result_img,(x,y),(x+w,y+h),(0,255,0),2) for cnt in contours_white: x,y,w,h = cv2.boundingRect(cnt) cv2.rectangle(result_img,(x,y),(x+w,y+h),(255,0,0),2) cv2.imshow("Result Image with Boxes", result_img) cv2.waitKey(0); cv2.destroyAllWindows(); ``` 以上脚本会先找出所有符合条件的形状轮廓,再逐一描绘矩形边框加以标注出来。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值