Skip to content

Instantly share code, notes, and snippets.

@ronalddas
Created February 7, 2016 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronalddas/650b43657701e80defe9 to your computer and use it in GitHub Desktop.
Save ronalddas/650b43657701e80defe9 to your computer and use it in GitHub Desktop.
Almost complete
#include<highgui/highgui.hpp>
#include<iostream>
#include<imgproc/imgproc.hpp>
#include<core/core.hpp>
#include<stdio.h>
#include<stdlib.h>
using namespace cv;
using namespace std;
int main(int argc , char **argv)
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
/*After giving the lowH values certain hue range regarding to that of
* red color, the image is atleast working.
* Need to make Something , that will let control this value's
* I think that's what trackbar do?
*/
int iLowH = 171;
int iHighH = 179;
int iLowS = 161;
int iHighS = 254;
int iLowV = 61;
int iHighV = 254;
int iLastX = -1;
int iLastY = -1;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
/*
namedWindow("Control",WINDOW_AUTOSIZE);
//Create trackbars in "Control" window
cvCreateTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
cvCreateTrackbar("HighH", "Control", &iHighH, 179);
cvCreateTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
cvCreateTrackbar("HighS", "Control", &iHighS, 255);
cvCreateTrackbar("LowV", "Control", &iLowV, 255); //Value (0 - 255)
cvCreateTrackbar("HighV", "Control", &iHighV, 255);
*/
//Nothing happened to the image after adding this!
Mat Image;
Image =imread(argv[1],CV_LOAD_IMAGE_UNCHANGED);
Mat ImageHSV;
cvtColor(Image , ImageHSV , COLOR_BGR2HSV);
//If i could increase the quality of HSV image, then my job would work.
//The white portion in HSV(actually red)is being shown by the inRange function.
namedWindow("Window",WINDOW_AUTOSIZE);
imshow("Window",ImageHSV);
Mat ImageThres;
//Problem is Not Here, image has been converted according to Value's
inRange(ImageHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV),ImageThres);
namedWindow("Window2",WINDOW_AUTOSIZE);
imshow("Window2",ImageThres);
//Morphological Opening is harming my image
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
//Detect Edges using threshold...
Mat Thres;
threshold(ImageThres,Thres,thresh,255,THRESH_BINARY);
//morphological closing (fill small holes in the foreground)
dilate( Thres, Thres, getStructuringElement(MORPH_ELLIPSE, Size(12, 12)) );
erode(Thres, Thres, getStructuringElement(MORPH_ELLIPSE, Size(12, 12)) );
namedWindow("Thres",WINDOW_AUTOSIZE);
imshow("Thres",Thres);
/// Find contours
findContours( Thres, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
//Contours has the location of the points... Now i have to draw a line along it.
/// Draw contours
/*Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
}
/// Show in a window
namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing );*/
/// Approximate contours to polygons + get bounding rects and circles
vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
vector<Point2f>center( contours.size() );
vector<float>radius( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{ approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
boundRect[i] = boundingRect( Mat(contours_poly[i]) );
minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] );
}
/// Draw polygonal contour + bonding rects + circles
Mat drawing = Mat::zeros( Thres.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
//rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );
}
/// Show in a window
namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing );
namedWindow("Original",WINDOW_AUTOSIZE);
imshow("Original",Image);
waitKey(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment