当前位置:网站首页>Opencv recent learning test code
Opencv recent learning test code
2022-04-22 06:10:00 【From deliberate to habit】
#include<opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat src,dst;
void CallBack_Demo(int, void*);
char out[] = "outPut";
int element_size = 3;
int max_size = 21;
int main()
{
// Display images
src = imread("C:\\Users\\siasun\\Desktop\\10.jpg");
if (src.empty())
{
cout << "counld not load image......" << endl;
return -1;
}
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", src);
// Displays a grayscale image
cvtColor(src, dst, CV_BGR2GRAY);
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", dst);
printf("input image channels:%d\n", dst.channels());
// Display image rows and columns
int cols = dst.cols;
int rows = dst.rows;
printf("rows:%d cols:%d\n", rows,cols);
const uchar*firstRow = dst.ptr<uchar>(0);
printf("input image channels:%d\n", *firstRow);
// Create a mat Images
Mat M(3, 3, CV_8UC3, Scalar(0, 0, 255));
//imshow("image", M);
// Create a blank image consistent with a graph size type
Mat m2 = Mat::zeros(dst.size(), dst.type());
//imshow("image", m2);
// Mask , Two methods of image enhancement
cols = src.cols * src.channels();
rows = src.rows;
int offset = src.channels();
Mat dst1 = Mat::zeros(src.size(), src.type());
/*for (int row = 1; row < rows; row++)
{
const uchar* current = src.ptr<uchar>(row);
const uchar* previous = src.ptr<uchar>(row - 1);
const uchar* next = src.ptr<uchar>(row + 1);
uchar* output = img.ptr<uchar>(row);
for (int col = offset; col < cols; col++)
output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offset] + current[col + offset] + previous[col] + next[col]));
}*/
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(src, dst1, src.depth(),kernel);
//imshow("image", dst1);
//imshow("image1", src);
// Read image pixels , Get contrast map
Mat gray;
cvtColor(src, gray, CV_BGR2GRAY);
int height, width;
height = gray.rows;
width = gray.cols;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int gray_value = gray.at<uchar>(row, col);
gray.at<uchar>(row, col) = 255 - gray_value;
}
}
// Multichannel contrast diagram
Mat grayMul,invertImg;
cvtColor(src, gray, CV_BGR2GRAY);
invertImg = Mat::zeros(src.size(), src.type());
int height1, width1;
height1 = gray.rows;
width1 = gray.cols;
int channel = src.channels();
for (int row = 0; row < height1; row++)
{
for (int col = 0; col < width1; col++)
{
if (channel == 1)
{
int gray_value = gray.at<uchar>(row, col);
gray.at<uchar>(row, col) = 255 - gray_value;
}
else
{
int chan1 = src.at<Vec3b>(row, col)[0];
int chan2 = src.at<Vec3b>(row, col)[1];
int chan3 = src.at<Vec3b>(row, col)[2];
invertImg.at<Vec3b>(row, col)[0] = 255 - chan1;
invertImg.at<Vec3b>(row, col)[1] = 255 - chan2;
invertImg.at<Vec3b>(row, col)[2] = 255 - chan3;
}
}
}
//opencv Contrast graph algorithm
bitwise_not(src, invertImg);// One operator does it
//Vec3b The order of the three channels is bgr Of uchar class
//Vec3f Corresponding float Type data
// hold cv_8uc1 go to cv32f1:src.convertTo(dst,CV_32F)
// The idea of taking the minimum of three numbers min(a,min(b,c))
// Image blending
/*int alpha = 0.5;// The weight
addWeighted(src, alpha, src1, (1 - alpha),dst);*/
char input_title[] = "input title";
// Blur the image
blur(src, dst, Size(5, 5), Point(-1, -1));
GaussianBlur(src, dst, Size(5, 5), 11, 11);
medianBlur(src, dst, 3);// Input , Output , Template size
// Bilateral ambiguity , Better protection of edge information
bilateralFilter(src, dst, 15, 100, 5);// Calculate the radius , Difference range ( Less than is used to calculate ), Template size
imshow(input_title, dst);
// inflation The maximum pixel value of the image covered by structural elements
// corrosion The minimum pixel value of the image covered by structural elements
//char out[] = "outPut";
namedWindow("out", CV_WINDOW_AUTOSIZE);
createTrackbar("element_size:", "out", &element_size, max_size, CallBack_Demo);
CallBack_Demo(0, 0);
// morphology
// Basic gradient Expansion minus corrosion
// Internal gradient Original subtraction
// Direction gradient x And y Gradient the direction
// Black hat The difference between the closed operation and the original image
// Top hat Open the difference image between the operation and the original image
Mat strucElement = getStructuringElement(MORPH_RECT, Size(3,3), Point(0, 0));
morphologyEx(src, dst, CV_MOP_TOPHAT, strucElement);
// Image pyramid
// On the sampling
pyrUp(src, dst, Size(src.cols * 2, src.rows * 2));
pyrDown(src, dst, Size(src.cols / 2, src.rows / 2));
// Gauss is different
Mat g1, g2, gr, dogImg;
cvtColor(src, gr, CV_BGR2GRAY);
GaussianBlur(gr, g1, Size(5, 5), 0, 0);
GaussianBlur(g1, g2, Size(5, 5), 0, 0);
subtract(g1, g2, dogImg, Mat());
//sobel edge detection
Mat gray_src;
GaussianBlur(src, dst, Size(3, 3), 0, 0);
cvtColor(dst, gray_src, CV_BGR2GRAY);
Mat xgrad, ygrad;
Sobel(gray_src, xgrad, CV_16S, 1, 0, 3);
Sobel(gray_src, ygrad, CV_16S, 0, 1, 3);
convertScaleAbs(xgrad, xgrad);// Take the absolute value
convertScaleAbs(ygrad, ygrad);
Mat xygray;
//x,y Assign weights
addWeighted(xgrad, 0.5, ygrad, 0.5,0, xygray,-1);
// Assign weights , Replace addweighted
xygray = Mat(xgrad.size(), xgrad.type());
int heigh = ygrad.rows;
int weigh = xgrad.cols;
for (int row = 0; row < heigh; row++)
{
for (int col = 0; col < weigh; col++)
{
int x = xgrad.at<char>(row, col);
int y = ygrad.at<char>(row, col);
int n = x + y;
xygray.at<char>(row, col) = saturate_cast<char>(n);
}
}
//another method Find edge
Scharr(gray_src, xgrad, CV_16S, 1, 0);
Scharr(gray_src, ygrad, CV_16S, 0, 1);
//laplance edge
Mat lap_img;
GaussianBlur(src, dst, Size(3, 3), 0, 0);
cvtColor(dst, gray_src, CV_BGR2GRAY);
Laplacian(gray_src, lap_img, CV_16S, 3);
convertScaleAbs(lap_img, lap_img);
threshold(lap_img, lap_img, 0, 255, THRESH_OTSU | THRESH_BINARY);
//canny edge
// Gaussian blur 、 Grayscale 、 gradient (sobel,scharr)、 Non maximum suppression 、 threshold
Canny(gray_src, dst, 20, 40, 3, false);
// Histogram equalization , Improve image contrast
equalizeHist(src, dst);//src It needs to be an eight bit single channel image
waitKey(0);
return 0;
}
void CallBack_Demo(int, void*)
{
int s = element_size * 2 + 1;
Mat strucElement = getStructuringElement(MORPH_RECT, Size(s,s), Point(0,0));
dilate(src, dst, strucElement, Point(-1, -1),1);// hinder 1 Refers to the number of expansion iterations
imshow("out", dst);
return;
}
版权声明
本文为[From deliberate to habit]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220542470954.html
边栏推荐
- Out range of signed 32bit display when compiling openssl-0.9.8e
- Chapter 88 leetcode sword refers to offer dynamic programming (V) maximum value of gifts
- jeecgboot-online在线开发3
- QWebsocket通信
- STM32 learning note 2 - set GPIO register to realize running water lamp
- LeetCode 898. Subarray bitwise OR operation - set
- 常见面试题 - 4(MySQL)
- Experience of constructing H-bridge with MOS tube
- AD5724 bipolar ADC
- stm32单片机与LD3320语音模块交互法二
猜你喜欢
随机推荐
关于手眼标定中RT矩阵的欧拉角和Halcon中pose的类型之间的关系
stm32单片机与LD3320语音模块交互法一
Chapter 86 leetcode refers to offer dynamic programming (III) maximum profit of stock
蓝桥杯嵌入式学习之双路AD采集
ADC
STM32 printf redirection virtual oscilloscope
File write data to local
QT学习之安装QT
CMake及交叉工具编译链的安装使用
WGS84 coordinate conversion, map picking, WGS84 coordinate tool recommended
Part 72 leetcode exercise (V) 5 Longest Palindromic Substring
Characteristics and usage of QT signal and slot
DS18B20 of Blue Bridge Cup embedded expansion board learning
Chapter 89 leetcode refers to offer dynamic programming (6) translating numbers into strings
蓝桥杯嵌入式扩展板学习之ADC按键
TXT text content is deleted one by one
Part 84 leetcode sword refers to offer dynamic programming (I) Fibonacci sequence
Code color difference of QT learning
蓝桥杯嵌入式扩展板学习之DS18B20
QT信号与槽的特点和用法









