OpenCV识别数码管方案之整体数字的轮廓识别

文章目录


一、思路

二、流程

1.滤波

(1)测试代码

https://blog.csdn.net/sandalphon4869/article/details/94725601
只要换一下图片就ok

(2)结论

选择高斯滤波更好,Size值为61.
GaussianBlur(g_srcImage,g_srcImage,Size(61,61),0.0);

2.颜色分离和二值化

(1)测试代码

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

String windowNames[]={"Blue","Green","Red"};
String trackbarNames[]={"ThresholdbarBlue","ThresholdbarGreen","ThresholdbarRed"};


Mat g_srcImage;

Mat g_dstImageBlue;
Mat g_dstImageGreen;
Mat g_dstImageRed;

Mat g_dstImageBlueThreshold;
Mat g_dstImageGreenThreshold;
Mat g_dstImageRedThreshold;

int g_dstImageBlueThresholdValue=0;
int g_dstImageGreenThresholdValue=0;
int g_dstImageRedThresholdValue=0;

const int g_dstImageThresholdValueMax=255;

vector<Mat> g_channels;

void mySplit()
{

    split(g_srcImage,g_channels);
    g_dstImageBlue=g_channels.at(0);
    g_dstImageGreen=g_channels.at(1);
    g_dstImageRed=g_channels.at(2);
}

void onChange(int,void*)
{
    //deep copy
    g_dstImageBlue.copyTo(g_dstImageBlueThreshold);
    g_dstImageGreen.copyTo(g_dstImageGreenThreshold);
    g_dstImageRed.copyTo(g_dstImageRedThreshold);
    
    //Threshold
    g_dstImageBlueThreshold=g_dstImageBlueThreshold>g_dstImageBlueThresholdValue;
    g_dstImageGreenThreshold=g_dstImageGreenThreshold>g_dstImageGreenThresholdValue;
    g_dstImageRedThreshold=g_dstImageRedThreshold>g_dstImageRedThresholdValue;

    //show
    imshow(windowNames[0],g_dstImageBlueThreshold);
    imshow(windowNames[1],g_dstImageGreenThreshold);
    imshow(windowNames[2],g_dstImageRedThreshold);
}

int main()
{
    g_srcImage=imread("M.jpg");
    GaussianBlur(g_srcImage,g_srcImage,Size(61,61),0.0);
    
    //split channels
    mySplit();

    //Window
    namedWindow(windowNames[0],WINDOW_NORMAL);
    namedWindow(windowNames[1],WINDOW_NORMAL);
    namedWindow(windowNames[2],WINDOW_NORMAL);

    //createTrackbar
    createTrackbar(trackbarNames[0],windowNames[0],&g_dstImageBlueThresholdValue,g_dstImageThresholdValueMax,onChange);
    createTrackbar(trackbarNames[1],windowNames[1],&g_dstImageGreenThresholdValue,g_dstImageThresholdValueMax,onChange);
    createTrackbar(trackbarNames[2],windowNames[2],&g_dstImageRedThresholdValue,g_dstImageThresholdValueMax,onChange);
    onChange(0,0);

	waitKey();
	return 0;
}

OpenCV识别数码管方案之整体数字的轮廓识别
OpenCV识别数码管方案之整体数字的轮廓识别
OpenCV识别数码管方案之整体数字的轮廓识别

(2)结论

选择红色的,阈值为160.

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

String windowNames[]={"Red"};
String trackbarNames[]={"ThresholdbarRed"};


Mat g_srcImage;

Mat g_dstImageRed;

Mat g_dstImageRedThreshold;


int g_dstImageRedThresholdValue=160;


vector<Mat> g_channels;

void mySplit()
{

    split(g_srcImage,g_channels);
    g_dstImageRed=g_channels.at(2);
}

void myThreshold()
{
    //deep copy
    g_dstImageRed.copyTo(g_dstImageRedThreshold);
    
    //Threshold
    g_dstImageRedThreshold=g_dstImageRedThreshold>g_dstImageRedThresholdValue;

    //show
    imshow(windowNames[0],g_dstImageRedThreshold);
}

int main()
{
    g_srcImage=imread("M.jpg");
    GaussianBlur(g_srcImage,g_srcImage,Size(61,61),0.0);
    
    //split channels
    mySplit();

    //Window
    namedWindow(windowNames[0],WINDOW_NORMAL);

    //threshold=160
    myThreshold();

	waitKey();
	return 0;
}

3.形态学运算

(1)测试代码

上一篇:css渲染层次理解及实际问题


下一篇:OpenCV3常用函数及功能汇总