Skip to content

Commit

Permalink
Merge pull request spmallick#281 from vishwesh5/master
Browse files Browse the repository at this point in the history
Fix Hough Transform Code
  • Loading branch information
spmallick authored Mar 20, 2019
2 parents 1a899e5 + 07138a4 commit 7c7e9d3
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 119 deletions.
Binary file added Hough-Transform/.hough_lines.py.swp
Binary file not shown.
36 changes: 36 additions & 0 deletions Hough-Transform/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cmake_minimum_required(VERSION 2.8.12)

PROJECT(HoughTransform)

######################## EDIT IF REQUIRED ####################
# ###Uncomment the line below and specify the path to OpenCV directory i.e. the path to the OpenCVConfig.cmake file. Check the examples given below.
#SET(OpenCV_DIR Enter-the-path-of-OpenCV-installation-on-your-system)


################### OpenCV_DIR Examples #####################

### MACOS : /usr/local/Cellar/opencv/3.3.1_1/share/OpenCV/

### UBUNTU : /usr/local/share/OpenCV/

### WINDOWS : C:\Users\yourname\Documents\opencv-3.3.1\build\install

##############################################################




################### ***DO NOT EDIT*** #####################

############# Common Instructions for all Users ############
find_package( OpenCV REQUIRED )

include_directories( ${OpenCV_INCLUDE_DIRS})

MACRO(add_example name)
ADD_EXECUTABLE(${name} ${name}.cpp)
TARGET_LINK_LIBRARIES(${name} ${OpenCV_LIBS} )
ENDMACRO()

add_example(hough_circles)
add_example(hough_lines)
28 changes: 23 additions & 5 deletions Hough-Transform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,40 @@

**Python**

- ```python3 hough_line.py road.png```
- ```python3 hough_lines.py lanes.jpg```

**C++**

- ```g++ hough_line.cpp `pkg-config opencv --cflags --libs` -o hough_line```
- ```./hough_line```
- ```g++ hough_lines.cpp `pkg-config opencv --cflags --libs` -o hough_lines```
- ```./hough_lines lanes.jpg```

**CMake**

- ```mkdir build```
- ```cd build```
- ```cmake ..```
- ```cmake --build . --config Release```
- ```cd ..```
- ```./build/hough_lines lanes.jpg```

### Hough Circle Transform

**Usage**

**Python**

- ```python3 hough_circles.py circles.png```
- ```python3 hough_circles.py brown-eyes.jpg```

**C++**

- ```g++ hough_circles.cpp `pkg-config opencv --cflags --libs` -o hough_circles```
- ```./hough_circles circles.png```
- ```./hough_circles brown-eyes.jpg```

**CMake**

- ```mkdir build```
- ```cd build```
- ```cmake ..```
- ```cmake --build . --config Release```
- ```cd ..```
- ```./build/hough_circles lanes.jpg```
66 changes: 36 additions & 30 deletions Hough-Transform/hough_circles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,68 +11,74 @@ using namespace cv;
using namespace std;

// Declare variables to store images
Mat dst, gray_src, cdstP, src;
Mat gray, cimg, img, edges;

int thresh;
const int thresh_max = 100;
double t;
int initThresh;
const int maxThresh = 200;
double p1,p2;

// Vector to store circle points
vector<Vec3f> circles;

void on_trackbar( int , void* ) {
cdstP = gray_src.clone();
// Apply hough transform
HoughCircles(cdstP, circles, HOUGH_GRADIENT, 1, cdstP.rows/64, 200, 10, 1, thresh);
void onTrackbarChange( int , void* ) {
cimg = img.clone();

p1 = initThresh;
p2 = initThresh * 0.4;

cdstP = src.clone();
// Detect circles using HoughCircles transform
HoughCircles(gray, circles, HOUGH_GRADIENT, 1, cimg.rows/64, p1, p2, 25, 50);

// Draw circle on points
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
circle( cdstP, center, radius, Scalar(255, 255, 255), 2, 8, 0 );
// Draw the outer circle
circle( cimg, center, radius, Scalar(0, 255, 0), 2);
// Draw the center of the circle
circle( cimg, center, 2, Scalar(0, 0, 255), 3);
}

cv::putText(cdstP, to_string(circles.size()), cv::Point(280, 60), cv::FONT_HERSHEY_SIMPLEX, 2, cv::Scalar(255, 0, 255), 4, cv::LINE_AA);
imshow( "Output-Image", cdstP);
// Display output image
imshow( "Image", cimg);

// Edge image for debugging
Canny(gray, edges, p1, p2);
imshow( "Edges", edges);
}


int main(int argc, char** argv){
const char* file = argv[1];
src = imread(file, IMREAD_COLOR);
cv::resize(src, src, cv::Size(400, 400));
img = imread(file, IMREAD_COLOR);

if(src.empty())
if(img.empty())
{
cout << "Error reading image" << file<< endl;
return -1;
}

// Remove noise using medianBlur
medianBlur(src, src, 3);

// Convert to gray-scale
cvtColor(src, gray_src, COLOR_BGR2GRAY);
cvtColor(img, gray, COLOR_BGR2GRAY);

// Will hold the results of the detection
namedWindow("Output-Image", 1);
// Will hold the results of the detection
namedWindow("Edges",1);
namedWindow("Image",1);

thresh = 10;
initThresh = 105;

createTrackbar("threshold", "Output-Image", &thresh, thresh_max, on_trackbar);
on_trackbar(thresh, 0);
createTrackbar("Threshold", "Image", &initThresh, maxThresh, onTrackbarChange);
onTrackbarChange(initThresh, 0);

imshow("source", src);
imshow("Image", img);
while(true)
{
int c;
c = waitKey( 0 );
if( (char)c == 27 )
int key;
key = waitKey( 0 );
if( (char)key == 27 )
{ break; }
}

destroyAllWindows();

}
4 changes: 2 additions & 2 deletions Hough-Transform/hough_circles.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def onTrackbarChange(max_slider):

if __name__ == "__main__":
# Read image
img = cv2.imread('brown-eyes.jpg', 1)
img = cv2.imread(sys.argv[1], 1)

# Convert to gray-scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Expand All @@ -55,7 +55,7 @@ def onTrackbarChange(max_slider):

while True:
key = cv2.waitKey(1)
if key == ord('q') or key == 27:
if key == 27:
break

cv2.destroyAllWindows()
77 changes: 0 additions & 77 deletions Hough-Transform/hough_line.cpp

This file was deleted.

86 changes: 86 additions & 0 deletions Hough-Transform/hough_lines.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <iostream>

using namespace cv;
using namespace std;

// variables to store images
Mat dst, cimg, gray, img, edges;

int initThresh;
const int maxThresh = 1000;
double th1,th2;

// create a vector to store points of line
vector<Vec4i> lines;

void onTrackbarChange( int , void* )
{
cimg = img.clone();
dst = img.clone();

th1 = initThresh;
th2 = th1 * 0.4;

Canny(img,edges,th1,th2);

// apply hough line transform
HoughLinesP(edges, lines, 2, CV_PI/180, 50, 10, 100);

// draw lines on the detected points
for( size_t i = 0; i < lines.size(); i++ )
{
Vec4i l = lines[i];
line( dst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 1, LINE_AA);
}

// show the resultant image
imshow("Result Image", dst);
imshow("Edges", edges);
}

int main(int argc, char** argv) {
const char* file = argv[1];
// Read image (color mode)
img = imread(file, 1);
dst = img.clone();

if(img.empty())
{
cout << "Error in reading image" << file<< endl;
return -1;
}

// Convert to gray-scale
cvtColor(img, gray, COLOR_BGR2GRAY);

// Detect edges using Canny Edge Detector
// Canny(gray, dst, 50, 200, 3);

// Make a copy of original image
// cimg = img.clone();

// Will hold the results of the detection
namedWindow("Edges",1);
namedWindow("Result Image", 1);

// Declare thresh to vary the max_radius of circles to be detected in hough transform
initThresh = 500;

// Create trackbar to change threshold values
createTrackbar("threshold", "Result Image", &initThresh, maxThresh, onTrackbarChange);
onTrackbarChange(initThresh, 0);

while(true)
{
int key;
key = waitKey( 1 );
if( (char)key == 27 )
{ break; }
}

destroyAllWindows();
}
2 changes: 1 addition & 1 deletion Hough-Transform/hough_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def onTrackbarChange(max_slider):

while True:
key = cv2.waitKey(1)
if key == ord('q') or key == 27:
if key == 27:
break

cv2.destroyAllWindows()
4 changes: 2 additions & 2 deletions InvisibilityCloak/Invisibility_Cloak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int main(int argc, char** argv){
cap >> background;
}

flip(background,background,1);
//flip(background,background,1);

while(1)
{
Expand All @@ -48,7 +48,7 @@ int main(int argc, char** argv){
break;

Mat hsv;
flip(frame,frame,1);
//flip(frame,frame,1);
cvtColor(frame, hsv, COLOR_BGR2HSV);

Mat mask1,mask2;
Expand Down
Loading

0 comments on commit 7c7e9d3

Please sign in to comment.