// opencv.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
// 全局变量定义及赋值
int threshold_type = 3;
int const max_type = 4; //阈值化类型
int threshold_Bvalue = 0; //B通道阈值设定
int threshold_Gvalue = 0; //G通道阈值设定
int threshold_Rvalue = 0; //R通道阈值设定
int threshold_Hvalue = 0; //H通道阈值设定
int threshold_Svalue = 0; //S通道阈值设定
int threshold_Vvalue = 0; //V通道阈值设定
int threshold_Lvalue = 0; //L通道阈值设定
int threshold_avalue = 0; //A通道阈值设定
int threshold_bvalue = 0; //B通道阈值设定
int threshold_Yvalue = 0; //L通道阈值设定
int threshold_Crvalue = 0; //A通道阈值设定
int threshold_Cbvalue = 0; //B通道阈值设定
int threshold_HHvalue = 0; //L通道阈值设定
int threshold_LLvalue = 0; //A通道阈值设定
int threshold_SSvalue = 0; //B通道阈值设定
int const max_value = 255;
int const max_BINARY_value = 255;
Mat src, dst;
Mat dst_Hsv, dst_Lab, dst_Ycrcb, dst_HLS;
//创建一个图像向量
vector<Mat> planes;
char* window_name = "Threshold Tool";
char* trackbar_type = "TrackbarType"; //0: Binary 1: Binary Inverted 2: Truncate
//3: To Zero 4: To Zero Inverted
char* trackbar_Bvalue = "B_Value";
char* trackbar_Gvalue = "G_Value";
char* trackbar_Rvalue = "R_Value";
char* trackbar_Hvalue = "H_Value";
char* trackbar_Svalue = "S_Value";
char* trackbar_Vvalue = "V_Value";
char* trackbar_Lvalue = "L";
char* trackbar_avalue = "a";
char* trackbar_bvalue = "b";
char* trackbar_Yvalue = "Y_Value";
char* trackbar_Crvalue = "Cr_Value";
char* trackbar_Cbvalue = "Cb_Value";
char* trackbar_HHvalue = "H_Value";
char* trackbar_LLvalue = "L_Value";
char* trackbar_SSvalue = "S_Value";
/// 自定义函数声明
void Threshold_Func( int, void* );
/* *************************** RGB颜色空间的阀值变化图像处理 ********************************* */
#if 0
int main()
{
//加载一幅图片
src = imread( "daitu.bmp", 1 );
// 将图片转换成灰度图片
//cvtColor( src, src_gray, CV_RGB2GRAY );
// 创建一个窗口显示图片
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
createTrackbar( trackbar_Bvalue,
window_name, &threshold_Bvalue,
max_value, Threshold_Func );
createTrackbar( trackbar_Gvalue,
window_name, &threshold_Gvalue,
max_value, Threshold_Func );
createTrackbar( trackbar_Rvalue,
window_name, &threshold_Rvalue,
max_value, Threshold_Func );
// 初始化自定义的阈值函数
Threshold_Func( 0, 0 );
// 等待用户按键。如果是ESC健则退出等待过程。
while(true)
{
int c;
c = waitKey( 20 );
if( (char)c == 27 )
{ break; }
}
}
//自定义的阈值函数
void Threshold_Func( int, void* )
{
Mat BW(src.rows, src.cols, CV_8UC1);
for(int i=0; i<src.rows; i++)
{
for(int j=0; j<src.cols;j++)
{
//printf("%f ", ttat<CV_32FC1>(i,j) );//if(img_h.at<CV_32F>(i,j))
if( ( (src.at<Vec3b>(i,j)[2] >= threshold_Rvalue) && (src.at<Vec3b>(i,j)[2] <= max_value) ) &&
( (src.at<Vec3b>(i,j)[1] >= threshold_Gvalue) && (src.at<Vec3b>(i,j)[1] <= max_value) ) &&
( (src.at<Vec3b>(i,j)[0] >= threshold_Bvalue) && (src.at<Vec3b>(i,j)[0] <= max_value) )
)
{
BW.at<uchar>(i,j) = 2;
}
else
{
BW.at<uchar>(i,j) = 1;
}
}
}
Mat BWR = BW-1;
Mat three_channel = Mat::zeros(BWR.rows,BWR.cols,CV_8UC3);
vector<Mat> channels;
for (int i=0;i<3;i++)
{
channels.push_back(BWR);
}
merge(channels,three_channel);
Mat ImageOut = Mat::zeros(BWR.rows,BWR.cols,CV_8UC3);
src.copyTo(ImageOut, three_channel);
imshow(window_name, ImageOut);
imwrite("zhongzi_RGB.bmp", ImageOut);
}
#endif
/* ******************* HSV颜色空间阀值变化图像处理 ******************* */
#if 0
int main()
{
//加载一幅图片
src = imread( "daitu.bmp", 1 );
// 将图片转换成HSV图片
cvtColor( src, dst_Hsv, CV_RGB2HSV );
imwrite("HSV.bmp", dst_Lab);
printf("mat type is: %d \n\r" , dst_Hsv.type());
// 创建一个窗口显示图片
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
createTrackbar( trackbar_Hvalue,
window_name, &threshold_Hvalue,
max_value, Threshold_Func );
createTrackbar( trackbar_Svalue,
window_name, &threshold_Svalue,
max_value, Threshold_Func );
createTrackbar( trackbar_Vvalue,
window_name, &threshold_Vvalue,
max_value, Threshold_Func );
// 初始化自定义的阈值函数
Threshold_Func( 0, 0 );
// 等待用户按键。如果是ESC健则退出等待过程。
while(true)
{
int c;
c = waitKey( 20 );
if( (char)c == 27 )
{ break; }
}
}
//自定义的阈值函数
void Threshold_Func( int, void* )
{
Mat BW(src.rows, src.cols, CV_8UC1);
for(int i=0; i<src.rows; i++)
{
for(int j=0; j<src.cols;j++)
{
//printf("%f ", ttat<CV_32FC1>(i,j) );//if(img_h.at<CV_32F>(i,j))
if( ( (dst_Hsv.at<Vec3b>(i,j)[0] >= threshold_Hvalue) && (dst_Hsv.at<Vec3b>(i,j)[0] <= max_value) ) &&
( (dst_Hsv.at<Vec3b>(i,j)[1] >= threshold_Svalue) && (dst_Hsv.at<Vec3b>(i,j)[1] <= max_value) ) &&
( (dst_Hsv.at<Vec3b>(i,j)[2] >= threshold_Vvalue) && (dst_Hsv.at<Vec3b>(i,j)[2] <= max_value) )
)
{
BW.at<uchar>(i,j) = 2;
}
else
{
BW.at<uchar>(i,j) = 1;
}
}
}
Mat BWR = BW-1;
Mat three_channel = Mat::zeros(BWR.rows,BWR.cols,CV_8UC3);
vector<Mat> channels;
for (int i=0;i<3;i++)
{
channels.push_back(BWR);
}
merge(channels,three_channel);
Mat ImageOut = Mat::zeros(BWR.rows,BWR.cols,CV_8UC3);
dst_Hsv.copyTo(ImageOut, three_channel);
imshow(window_name, ImageOut);
imwrite("zhongzi_HSG.bmp", ImageOut);
}
#endif
/* ******************* Lab颜色空间阀值变化图像处理 ******************* */
#if 1
int main()
{
//加载一幅图片
src = imread( "original.bmp", 1 );
// 将图片转换成HSV图片
cvtColor( src, dst_Lab, CV_RGB2Lab );
imwrite("picLab.bmp", dst_Lab);
// printf("mat type is: %d \n\r" , dst_Lab.type());
// 创建一个窗口显示图片
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
createTrackbar( trackbar_Lvalue,
window_name, &threshold_Lvalue,
max_value, Threshold_Func );
createTrackbar( trackbar_avalue,
window_name, &threshold_avalue,
max_value, Threshold_Func );
createTrackbar( trackbar_bvalue,
window_name, &threshold_bvalue,
max_value, Threshold_Func );
// 初始化自定义的阈值函数
Threshold_Func( 0, 0 );
// 等待用户按键。如果是ESC健则退出等待过程。
while(true)
{
int c;
c = waitKey( 20 );
if( (char)c == 27 )
{ break; }
}
}
//自定义的阈值函数
void Threshold_Func( int, void* )
{
Mat BW = Mat::zeros(src.rows,src.cols,CV_8UC1);//将BW初始化为全零矩阵,否则矩阵内可能是随机值
for(int i=0; i<src.rows; i++)
{
for(int j=0; j<src.cols;j++)
{
//printf("%f ", ttat<CV_32FC1>(i,j) );//if(img_h.at<CV_32F>(i,j))
if( ( (dst_Lab.at<Vec3b>(i,j)[0] >= threshold_Lvalue) && (dst_Lab.at<Vec3b>(i,j)[0] <= max_value) ) &&
( (dst_Lab.at<Vec3b>(i,j)[1] >= threshold_avalue) && (dst_Lab.at<Vec3b>(i,j)[1] <= max_value) ) &&
( (dst_Lab.at<Vec3b>(i,j)[2] >= threshold_bvalue) && (dst_Lab.at<Vec3b>(i,j)[2] <= max_value) )
)
{
//将大于阈值的像素点的位置都标记为1,后边将根据此位置来生成掩码信息
BW.at<uchar>(i,j) = 1;
}
}
}
//Mat three_channel = Mat::zeros(BW
没有合适的资源?快使用搜索试试~ 我知道了~
使用 opencv 将图像转换到不同的颜色空间,并进行图像分割.zip
共1个文件
cpp:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 58 浏览量
2023-09-19
16:09:54
上传
评论
收藏 3KB ZIP 举报
温馨提示
使用 opencv 将图像转换到不同的颜色空间,并进行图像分割.zip
资源推荐
资源详情
资源评论
收起资源包目录
使用 opencv 将图像转换到不同的颜色空间,并进行图像分割.zip (1个子文件)
T
使用 opencv 将图像转换到不同的颜色空间,并进行图像分割
opencv.cpp 12KB
共 1 条
- 1
资源评论
处处清欢
- 粉丝: 159
- 资源: 2521
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功