出入每天学习点OpenCV,做做计算机视觉项目
为了项目与论文需要,现在入门 OpenCV4学习,并且学习点深度学习的东西,希望有助于自身论文的研究,只是为了毕业。。
提示:以下是本篇文章正文内容,下面案例可供参考
void cvCvtColor( const CvArr* src, CvArr* dst, int code );
src:源图像(输入的 8-bit , 16-bit 或 32-bit 单倍精度浮点数影像)
dst:目标图像(输入的 8-bit , 16-bit 或 32-bit 单倍精度浮点数影像)
code:色彩转换
CV_BGR2BGRA、CV_RGB2BGRA、CV_BGRA2RGBA、CV_BGR2BGRA、CV_BGRA2BGR
CV_RGB2GRAY、CV_GRAY2RGB、CV_RGBA2GRAY、CV_GRAY2RGBA
CV_BGR2HSV、CV_RGB2HSV、CV_HSV2BGR、CV_HSV2RGB
CV_BGR2HLS、CV_RGB2HLS、CV_HLS2BGR、CV_HLS2RGB
void OpenCV_quick_day01Demo::colorSpacedDemo(Mat &image) {
Mat gray, hsv;
//色彩空间转换,转灰度图,转hsv
cvtColor(image, gray, COLOR_BGR2GRAY);
cvtColor(image, hsv, COLOR_BGR2HSV);
//H 0-180 S,V 0-255, H,S表示颜色, V表示亮度
namedWindow("gray", WINDOW_FREERATIO);
imshow("gray", gray);
namedWindow("hsv", WINDOW_FREERATIO);
imshow("hsv", hsv);
void OpenCV_quick_day01Demo::creatMatDemo(Mat &image) {
Mat m1, m2, m3, m4;
//对于Mat,复制图像,复制了一个对象
//复制图像-克隆
m1 = image.clone();
imshow("m1", m1);
//复制图像-复制
image.copyTo(m2);
imshow("m2", m2);
//对于Mat,对图像赋值,只是创建指针指向同一个对象
//赋值
m3 = image;
imshow("m3", m3);
//创建空白图像
//Mat::zeros 赋值为0
m4 = Mat::zeros(Size(8, 8), CV_8UC3);//创建8x8图像, CV_8UC1表示单通道 CV_8UC2双通道,CV_8UC3表示三通道等;
std::cout << "width:" << m4.cols << " height:" << m4.rows
<< " channels:" << m4.channels() << std::endl;
std::cout << m4 << std::endl;
//Mat::ones 对第一通道赋值为1
Mat m5 = Mat::ones(Size(8, 8), CV_8UC3);
std::cout << "width:" << m5.cols << " height:" << m5.rows
<< " channels:" << m5.channels() << std::endl;
std::cout << m5 << std::endl;
//Scalar(x,x,x)对mat各个通道赋值为X
Mat m6 = Mat::zeros(Size(8, 8), CV_8UC3);
m6 = Scalar(127, 127, 127);
std::cout << "width:" << m6.cols << " height:" << m6.rows
<< " channels:" << m6.channels() << std::endl;
std::cout << m6 << std::endl;
}
void OpenCV_quick_day01Demo::pixelVisitDemo(Mat &image) {
int w = image.cols;
int h = image.rows;
int channel = image.channels();
//基于数组下标操作图像像素
/*for (int col = 0; col < w ; col++)
{
for (int row = 0; row < h; row++)
{
if (channel == 1)//灰度图像操作
{
int pv = image.at(row, col);
image.at(row, col) = 255 - pv;
}
else if (channel == 3)//彩色图像
{
Vec3b bgr = image.at(row, col);
image.at(row, col)[0] = 255 - bgr[0];
image.at(row, col)[1] = 255 - bgr[1];
image.at(row, col)[2] = 255 - bgr[2];
}
}
}
imshow("piexlVisit", image);*/
//基于指针操作图像像素
for (int row = 0; row < h; row++)
{
uchar *current_row = image.ptr<uchar>(row);
for (int col = 0; col < w; col++)
{
if (channel == 1)//灰度图像操作
{
int pv = *current_row;
*current_row++ = 255 - pv;
}
else if (channel == 3)//彩色图像
{
*current_row++ = 255 - *current_row;
*current_row++ = 255 - *current_row;
*current_row++ = 255 - *current_row;
}
}
}
imshow("piexlVisitPtr", image);
}
void OpenCV_quick_day01Demo::color_style_demo(Mat &image) {
namedWindow("颜色风格", WINDOW_AUTOSIZE);
int colorMap[] = { COLORMAP_AUTUMN,COLORMAP_BONE,COLORMAP_CIVIDIS,COLORMAP_COOL,
COLORMAP_DEEPGREEN,COLORMAP_HOT,COLORMAP_HSV,COLORMAP_INFERNO,COLORMAP_INFERNO,
COLORMAP_JET,COLORMAP_MAGMA,COLORMAP_OCEAN,COLORMAP_PARULA,COLORMAP_PINK,COLORMAP_PLASMA,
COLORMAP_RAINBOW,COLORMAP_SPRING,COLORMAP_SUMMER,COLORMAP_TURBO,COLORMAP_TWILIGHT,
COLORMAP_TWILIGHT_SHIFTED,COLORMAP_VIRIDIS,COLORMAP_WINTER };
Mat dst;
int index = 0;
while (true) {
int c = waitKey(1000);
if (c == 27) {
break;
}
applyColorMap(image, dst, colorMap[index % 23]);
index++;
imshow("颜色风格", dst);
}
}
void OpenCV_quick_day01Demo::bitwise_demo(Mat &image) {
Mat m1 = Mat::zeros(Size(256, 256), CV_8UC3);
Mat m2 = Mat::zeros(Size(256, 256), CV_8UC3);
rectangle(m1, Rect(100, 100, 100, 100), Scalar(255, 255, 0), -1, LINE_8, 0);
rectangle(m2, Rect(150, 150, 100, 100), Scalar(0, 255, 255), -1, LINE_8, 0);
imshow("m1", m1);
imshow("m2", m2);
Mat dst;
bitwise_and(m1, m2, dst);
imshow("与操作", dst);
bitwise_or(m1, m2, dst);
imshow("或操作", dst);
bitwise_not(m1, dst);
imshow("非操作", dst);
bitwise_xor(m1, m2, dst);
imshow("异或操作", dst);
}