1.处理边缘
代码演示:
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
#include <opencv2\imgproc\types_c.h>
using namespace cv;
int main(int argc, char** argv)
{
Mat src, dst;
src = imread("C:/ERRT/140506-jordan-speedread-tease_ednmry.jpg");
if (!src.data)
{
printf("could not load image...\n");
return -1;
}
char INPUT_WIN[] = "input image";
char OUTPUT_WIN[] = "Border Demo";
namedWindow(INPUT_WIN, WINDOW_FREERATIO);
namedWindow(OUTPUT_WIN, WINDOW_FREERATIO);
imshow(INPUT_WIN, src);
int top = (int)(0.05 * src.rows);
int bottom = (int)(0.05 * src.rows);
int left = (int)(0.05 * src.cols);
int right = (int)(0.05 * src.cols);
RNG rng(12345);
int borderType = BORDER_DEFAULT;
int c = 0;
while (true)
{
c = waitKey(500);
//ESC
if ((char)c == 27)
{
break;
}
if ((char)c == 'r')
{
borderType = BORDER_REPLICATE;
}
else if((char)c=='w')
{
borderType = BORDER_WRAP;
}
else if ((char)c == 'c')
{
borderType = BORDER_CONSTANT;
}
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
copyMakeBorder(src, dst, top, bottom, left, right, borderType, color);
imshow(OUTPUT_WIN, dst);
}
return 0;
}
效果图:
2.Sobel算子
代码演示:
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
#include <opencv2\imgproc\types_c.h>
using namespace cv;
int main(int argc, char** argv)
{
Mat src, dst;
src = imread("C:/ERRT/140506-jordan-speedread-tease_ednmry.jpg");
if (!src.data)
{
printf("could not load image...\n");
return -1;
}
char INPUT_WIN[] = "input image";
char OUTPUT_WIN[] = "sobel-demo";
namedWindow(INPUT_WIN, WINDOW_FREERATIO);
namedWindow(OUTPUT_WIN, WINDOW_FREERATIO);
imshow(INPUT_WIN, src);
Mat gray_src;
GaussianBlur(src, dst, Size(3, 3), 0, 0);
cvtColor(dst, gray_src, CV_BGR2GRAY);
namedWindow("gray image", WINDOW_FREERATIO);
imshow("gray image", gray_src);
Mat xgrad, ygrad;
Sobel(gray_src, xgrad, CV_16S, 1, 0, 3);
Sobel(gray_src, ygrad, CV_16S, 0, 1, 3);
convertScaleAbs(xgrad, xgrad);
convertScaleAbs(ygrad, ygrad);
namedWindow("xgrad", WINDOW_FREERATIO);
namedWindow("ygrad", WINDOW_FREERATIO);
imshow("xgrad", xgrad);
imshow("ygrad", ygrad);
Mat xygrad;
addWeighted(xgrad, 0.5, ygrad, 0.5, 0, xygrad);
imshow(OUTPUT_WIN, xygrad);
waitKey(0);
return 0;
}
?效果图:
3.Laylance算子
代码演示:
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
#include <opencv2\imgproc\types_c.h>
using namespace cv;
int main(int argc, char** argv)
{
Mat src, dst;
src = imread("C:/ERRT/140506-jordan-speedread-tease_ednmry.jpg");
if (!src.data)
{
printf("could not load image...\n");
return -1;
}
char INPUT_WIN[] = "input image";
char OUTPUT_WIN[] = "Laplance Result";
namedWindow(INPUT_WIN, WINDOW_FREERATIO);
imshow(INPUT_WIN, src);
Mat gray_src, edg_image;
GaussianBlur(src, dst, Size(3, 3), 0, 0);
cvtColor(dst, gray_src, CV_BGR2GRAY);
Laplacian(gray_src, edg_image, CV_16S, 3);
convertScaleAbs(edg_image, edg_image);
threshold(edg_image, edg_image, 0, 255, THRESH_OTSU | THRESH_BINARY);
namedWindow(OUTPUT_WIN, WINDOW_FREERATIO);
imshow(OUTPUT_WIN, edg_image);
waitKey(0);
return 0;
}
?效果图:
4.Canny-边缘检测
代码演示:
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
#include <opencv2\imgproc\types_c.h>
using namespace cv;
Mat src, gray_src, dst;
int t1_value = 20;
int max_value = 255;
const char* OUTPUT_TIITLE = "Canny Result";
void Canny_Demo(int, void*);
int main(int argc, char** argv)
{
src = imread("C:/ERRT/140506-jordan-speedread-tease_ednmry.jpg");
if (!src.data)
{
printf("could not load image...\n");
return -1;
}
char INPUT_TIITLE[] = "input image";
namedWindow(INPUT_TIITLE, WINDOW_FREERATIO);
namedWindow(OUTPUT_TIITLE, WINDOW_FREERATIO);
imshow(INPUT_TIITLE, src);
cvtColor(src, gray_src, CV_BGR2GRAY);
createTrackbar("Threshold Value:", OUTPUT_TIITLE, &t1_value, max_value, Canny_Demo);
Canny_Demo(0, 0);
waitKey(0);
return 0;
}
void Canny_Demo(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());
src.copyTo(dst, edge_output);
imshow(OUTPUT_TIITLE, dst);
}
?效果图:
?
|