图像处理基础2
Mat src, dst;
int threshold_value = 127;
int threshold_max = 255;
const char* output_title = "binary image";
void Threshold_dome(int, void*);
Mat gray_src;
int type_value = 2;
int type_max = 5;
int main(int argc, char** argv) {
// 形态学
char output_win[] = "output image";
src = imread("D:/opencvsp/pic.png");
if (!src.data)
{
printf("could not load image..\n");
return -1;
}
namedWindow("input img", WINDOW_AUTOSIZE);
imshow("input img", src);
//上采样
pyrUp(src, dst, Size(src.cols * 2, src.rows * 2));
//降采样
pyrDown(src, dst, Size(src.cols / 2, src.rows / 2));
//DOG
Mat gray_src, g1, g2, dogImg;
cvtColor(src, gray_src, COLOR_BGR2GRAY);
GaussianBlur(gray_src, g1, Size(3, 3), 0, 0);
GaussianBlur(g1, g2, Size(3, 3), 0);
subtract(g1, g2, dogImg, Mat());
//归一化
normalize(dogImg, dogImg, 255, 0, NORM_MINMAX);
imshow("dogImg", dogImg);
///基本阈值操作
createTrackbar("threshold value:", output_title,&threshold_value,threshold_max, Threshold_dome);
createTrackbar("threshold value:", output_title, &type_value, type_max, Threshold_dome);
waitKey(0);
return 0;
}
void Threshold_Demo(int, void*) {
cvtColor(src, gray_src, COLOR_BGR2GRAY);
//threshold(gray_src, dst, threshold_value, threshold_max, type_value);
threshold(gray_src, dst, 0, 255, THRESH_OTSU|type_value); //THRESH_TRIANGLE
imshow(output_title, dst);
}
Mat src, dst;
// sobel算子
char output_win[] = "output image";
src = imread("D:/opencvsp/pic.png");
if (!src.data)
{
printf("could not load image..\n");
return -1;
}
namedWindow("input img", WINDOW_AUTOSIZE);
imshow("input img", src);
//sobelx 方向
Mat kernel_x = (Mat_<int>(3, 3) << -1, 0, -1, -2, 0, 2, -1, 0, 1);
filter2D(src, dst, -1, kernel_x, Point(-1, -1), 0.0);
//Sobel Y 方向
Mat ying;
Mat kernel_y = (Mat_<int>(3, 3) << 0, -1, 0, -1, 4, -1, 0, -1, 0);
filter2D(src, dst, -1, kernel_y, Point(-1, -1), 0.0);
imshow(output_win, dst);
int ksize;
int c = 0;
int index = 0;
while (true)
{
c = waitKey(500);
if ((char)c == 27)
{
break;
}
ksize = 4 + (index % 8) * 2 + 1;
Mat kernel = Mat::ones(Size(ksize,ksize),CV_32F/(float)(ksize * ksize));
filter2D(src, dst, -1, kernel, Point(-1, -1));
index++;
imshow(output_win,dst);
}
Mat src, dst,gray_src;
int t1_value = 50;
int max_value = 255;
int type_max = 5;
int main(int argc, char** argv) {
src = imread("D:/opencvsp/pic.png");
if (!src.data)
{
printf("could not load image..\n");
return -1;
}
//Laplacian
//Canny边缘检测
cvtColor(src, gray_src, COLOR_BGR2GRAY);
createTrackbar("threshold", "output", &t1_value, max_value, Canny_Demoe);
waitKey(0);
return 0;
}
void Canny_Demoe(int, void*) {
Mat edge_output;
blur(gray_src, gray_src, Size(3, 3), Point(-1, -1), BORDER_DEFAULT);
Canny(gray_src, edge_output, t1_value, t1_value * 2, 3, false);
dst.create(src.size(), src.type());
Mat mask1 = Mat::zeros(src.size(), src.type());
src.copyTo(dst, edge_output);
}
//霍夫变换直线
//Canny边缘检测
Canny(src, gray_src, 100, 200);
cvtColor(gray_src, dst, COLOR_GRAY2BGR);
std::vector<Vec4f> plines;
HoughLinesP(gray_src, plines, 1, CV_PI / 180.0, 10, 0, 0);
Scalar color = Scalar(0, 0, 255);
for (size_t i = 0; i < plines.size(); i++)
{
Vec4f hline = plines[i];
line(dst, Point(hline[0], hline[1]), Point(hline[2], hline[3]), color, 3, LINE_AA);
}
imshow("outputtitle", dst);
//霍夫变换直线
//Canny边缘检测
Canny(src, gray_src, 100, 200);
cvtColor(gray_src, dst, COLOR_GRAY2BGR);
std::vector<Vec4f> plines;
HoughLinesP(gray_src, plines, 1, CV_PI / 180.0, 10, 0, 0);
Scalar color = Scalar(0, 0, 255);
for (size_t i = 0; i < plines.size(); i++)
{
Vec4f hline = plines[i];
line(dst, Point(hline[0], hline[1]), Point(hline[2], hline[3]), color, 3, LINE_AA);
}
imshow("outputtitle", dst);
//霍夫变换园检测
Mat moutout;
medianBlur(src, moutout, 3);
cvtColor(moutout, moutout, COLOR_BGR2GRAY);
std::vector<Vec3f> pcircles;
HoughCircles(moutout, pcircles, HOUGH_GRADIENT, 1, 10, 100, 30, 5, 50);
src.copyTo(dst);
for (size_t i = 0; i < pcircles.size(); i++)
{
Vec3f cc = pcircles[i];
circle(dst, Point(cc[0], cc[1]), cc[2], Scalar(0, 0, 255), 2, LINE_AA);
circle(dst, Point(cc[0], cc[1), 2, Scalar(198, 23, 155), 2, LINE_AA);
}
imshow("outputtitle", dst);