Skip to content

Instantly share code, notes, and snippets.

@sidd607
Created February 7, 2016 14:25
Show Gist options
  • Save sidd607/ec455d6e2f0c8f4fa27e to your computer and use it in GitHub Desktop.
Save sidd607/ec455d6e2f0c8f4fa27e to your computer and use it in GitHub Desktop.
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
Mat src, src_gray;
Mat dst, detected_edges;
int edgeThresh = 1;
int lowThreshold;
int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
char* window_name = "Edge Map";
int sobel(Mat image) {
imshow("original", image);
Mat src, src_gray;
Mat grad;
char* window_name = "Sobel Demo - Simple Edge Detector";
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
int c;
/// Load an image
src = image;
if (!src.data)
{
return -1;
}
GaussianBlur(src, src, Size(3, 3), 0, 0, BORDER_DEFAULT);
/// Convert it to gray
cvtColor(src, src_gray, CV_BGR2GRAY);
/// Create window
namedWindow(window_name, CV_WINDOW_AUTOSIZE);
/// Generate grad_x and grad_y
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
/// Gradient X
//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
Sobel(src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT);
convertScaleAbs(grad_x, abs_grad_x);
/// Gradient Y
//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel(src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT);
convertScaleAbs(grad_y, abs_grad_y);
/// Total Gradient (approximate)
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
imshow(window_name, grad);
waitKey(0);
return 0;
}
void cannyThreshold(int, void*) {
/// Reduce noise with a kernel 3x3
blur(src_gray, detected_edges, Size(3, 3));
/// Canny detector
Canny(detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size);
/// Using Canny's output as a mask, we display our result
dst = Scalar::all(0);
src.copyTo(dst, detected_edges);
imshow(window_name, dst);
}
int canny(Mat image) {
src = image;
if (!src.data)
{
return -1;
}
/// Create a matrix of the same type and size as src (for dst)
dst.create(src.size(), src.type());
/// Convert the image to grayscale
cvtColor(src, src_gray, CV_BGR2GRAY);
/// Create a window
namedWindow(window_name, CV_WINDOW_AUTOSIZE);
/// Create a Trackbar for user to enter threshold
createTrackbar("Min Threshold:", window_name, &lowThreshold, max_lowThreshold, cannyThreshold);
/// Show the image
cannyThreshold(0, 0);
/// Wait until user exit program by pressing a key
waitKey(0);
return 0;
}
int kMeans(Mat image, int count) {
if (!image.data)
return 1;
Mat src = image;
imshow("original", image);
Mat samples(src.rows * src.cols, 3, CV_32F);
for (int y = 0; y < src.rows; y++)
for (int x = 0; x < src.cols; x++)
for (int z = 0; z < 3; z++)
samples.at<float>(y + x*src.rows, z) = src.at<Vec3b>(y, x)[z];
int clusterCount = count;
Mat labels;
int attempts = 5;
Mat centers;
kmeans(samples, clusterCount, labels, TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10000, 0.0001), attempts, KMEANS_PP_CENTERS, centers);
Mat new_image(src.size(), src.type());
for (int y = 0; y < src.rows; y++)
for (int x = 0; x < src.cols; x++)
{
int cluster_idx = labels.at<int>(y + x*src.rows, 0);
new_image.at<Vec3b>(y, x)[0] = centers.at<float>(cluster_idx, 0);
new_image.at<Vec3b>(y, x)[1] = centers.at<float>(cluster_idx, 1);
new_image.at<Vec3b>(y, x)[2] = centers.at<float>(cluster_idx, 2);
}
imshow("clustered image", new_image);
waitKey(0);
return 0;
}
int main() {
Mat image = imread("C:/Users/siddartha/Downloads/im/test/14037.jpg");
//sobel(image);
//canny(image);
kMeans(image,2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment