Detecting Road from satellite image in OpenCV - c++

I'm trying to detect roads from a satellite image! It can be any type of paved/asphalt road. I'm using OpenCV! The way I am trying is, first I used Canny to detect the edges, the I am trying to apply hough transform to detect the straight lines from the edges. Then I planned to detect circles to detect the curves of the roads. The problem I'm facing is, after doing canny, hough is not working. Here is my code so far:
#include <iostream>
#include <math.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main()
{
//read an image
cv::Mat image = cv::imread("img2.jpg");
//create image window named "My Image"
cv::namedWindow("My Image");
//show the image on the window
cv::imshow("My Image", image);
cv::Mat contours,hough;
//canny
cv::Canny(image, contours, 0, 100);
cv::namedWindow("My Image2");
cv::imshow("My Image2", contours);
// Hough tranform for line detection
std::vector<cv::Vec2f> lines;
cv::HoughLines(contours, lines, 1, CV_PI/180, 80, 0, 0);
std::vector<cv::Vec2f>::const_iterator it= lines.begin();
while(it!=lines.end())
{
float rho= (*it)[0];
// first element is distance rho
float theta= (*it)[1];
//second element is angle theta
if(theta < CV_PI/4. || theta > 3.*CV_PI/4.)
{ // ~vertical line
// point of intersection of the line with first row
cv::Point pt1(rho/cos(theta),0);
// point of intersection of the line with last row
cv::Point pt2((rho-result.rows*sin(theta))/cos(theta),result.rows);
// draw a white line
cv::line( image, pt1, pt2, cv::Scalar(255), 1);
}
else
{ // ~horizontal line
// point of intersection of the
// line with first column
cv::Point pt1(0,rho/sin(theta));
// point of intersection of the line with last column
cv::Point pt2(result.cols,(rho-result.cols*cos(theta))/sin(theta));
// draw a white line
cv::line(image, pt1, pt2, cv::Scalar(255), 1);
}
++it;
}
//show lines
cv::namedWindow("My Image3");
cv::imshow("My Image3", hough);
//wait key for 5000ms
cv::waitKey(80000);
return 0;
}
In this code, I'm getting an error under "result" at line cv::Point pt2((rho-result.rows*sin(theta))/cos(theta),result.rows);
And another error under "result" at line cv::Point pt2(result.cols,(rho-result.cols*cos(theta))/sin(theta));
I can't figure it out what errors are those and what are the solutions! Any help would be appreciated :)
Also I'm totally new at computer vision, if you have any other easier and simpler way to accomplish my goal please feel free to share it :)
For your help I'm giving the sample image and the image of errors:
https://drive.google.com/open?id=0B4qYRvG5emZDWXhNV2V2djZWdUk
Error is for the errors, Canny is after performing canny algorithm and input is the sample image I took!
NOTE: I'm following OpenCV cookbook for my codes!

Related

How to detect the circles in the image using opencv 3 in c++

How Can I detect the circles and count the number in this image. I'm new to open cv and c++.Can any one help with this issue. I tried with hough circle . But didn't work .
The skeletonized binary image is as follows.
Starting from this image (I removed the border):
You can follow this approach:
1) Use findContour to get the contours.
2) Keep only internal contours. You can do that checking the sign of the area returned by contourArea(..., true). You'll get the 2 internal contours:
3) Now that you have the two contours, you can find a circle with minEnclosingCircle (in blue), or fit an ellipse with fitEllipse (in red):
Here the full code for reference:
#include <opencv2/opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
// Get contours
vector<vector<Point>> contours;
findContours(img, contours, RETR_TREE, CHAIN_APPROX_NONE);
// Create output image
Mat3b out;
cvtColor(img, out, COLOR_GRAY2BGR);
Mat3b outContours = out.clone();
// Get internal contours
vector<vector<Point>> internalContours;
for (size_t i = 0; i < contours.size(); ++i) {
// Find orientation: CW or CCW
double area = contourArea(contours[i], true);
if (area >= 0) {
// Internal contour
internalContours.push_back(contours[i]);
// Draw with different color
drawContours(outContours, contours, i, Scalar(rand() & 255, rand() & 255, rand() & 255));
}
}
// Get circles
for (const auto& cnt : internalContours) {
Point2f center;
float radius;
minEnclosingCircle(cnt, center, radius);
// Draw circle in blue
circle(out, center, radius, Scalar(255, 0, 0));
}
// Get ellipses
for (const auto& cnt : internalContours) {
RotatedRect rect = fitEllipse(cnt);
// Draw ellipse in red
ellipse(out, rect, Scalar(0, 0, 255), 2);
}
imshow("Out", out);
waitKey();
return 0;
}
First of all you have to find all contours at your image (see function cv::findContours).
You have to analyse these contours (check it for accordance to your requirements).
P.S. The figure at the picture is definitely not circle. So I can't say exactly how do you have to check received contours.

Detecting Circle Using Hough Transform

I'm trying to detect circles with using hough transform.
With my current code I can detect the one below
But I want to find black hole inside the circle I've detected.
however changing parameters of houghcircle method is not helped me. Actually it found circles that are not exist.
Also I've tried crop the circle I've found and do another hough transform on this new part it also didn't help me.
here is my code
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/opencv.hpp" // needs imgproc, imgcodecs & highgui
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src, circleroi;
/// Read the image
src = imread( "/Users/Rodrane/Documents/XCODE/test/mkedenemeleri/alev/delikli/gainfull.jpg", 2 );
/// Convert it to gray
// cvtColor( src, src_gray, CV_BGR2GRAY );
/// Reduce the noise so we avoid false circle detection
GaussianBlur( src, src, Size(3, 3), 2, 2 );
// adaptiveThreshold(src,src,255,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY,9,14);
vector<Vec3f> circles,circlessmall;
// Canny( src, src, 50 , 70, 3 );
/// Apply the Hough Transform to find the circles
HoughCircles( src, circles, CV_HOUGH_GRADIENT, 1, src.rows/8, 200, 100, 0, 0 );
/// Draw the circles detected
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][4]));
int radius = cvRound(circles[i][5]);
// circle center
circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
// circle outline
circle( src, center, radius, Scalar(0,255,0), 3, 8, 0 );
circleroi = src(Rect(center.x - radius, // ROI x-offset, left coordinate
center.y - radius, // ROI y-offset, top coordinate
2*radius, // ROI width
2*radius));
// imshow( "Hough Circle Transform Demo", circleroi );
}
resize(src, src, Size(src.cols/2, src.rows/2));
// threshold( circleroi, circleroi, 50, 255,CV_THRESH_BINARY );
// cout<<circleroi<<endl;
imshow("asd",src);
// imwrite("/Users/Rodrane/Documents/XCODE/test/mkedenemeleri/alev/cikti/deliksiz.jpg",circleroi);
waitKey(0);
return 0;
}
Update: since hough uses canny inside I'm manually used canny to see wether it finds the circle or not.
here canny results with
Canny(src,src, 100, 200,3);
thank you
You're setting one of the HoughCircles parameters minDist = src.rows/8, which is fairly large. The docs explain:
minDist – Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed.
The method can't return both the circle that it does find and the circle that you want, since they have nearly the same center (to within src.rows/8), just different sizes. If you set maxRadius to a value around 30 in order to exclude the larger circle, do you get the desired smaller circle?

Lane Detector divider lines c ++ with OpenCV

Now I have been working on the analysis of images with OpenCV, what I'm trying to do is recognize the lane dividing lines, what I do is the following:
1.I receive a image,
2. Then transform it to grayscale
3.I apply the GaussianBlur
4.After I place me in the ROI
5.I apply the canny
6.then I look for lines with hough transform Lines
7.Draw the lines obtained from hough
But I've run into a problem which is:
that recognizes no dividing lines both rail and neither recognizes the yellow lines.
I hope to help me solve this problem, you will thank a lot.
Then I put the code
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>
#include <stdio.h>
#include "linefinder.h"
using namespace cv;
int main(int argc, char* argv[]) {
int houghVote = 200;
string arg = argv[1];
Mat image;
image = imread(argv[1]);
Mat gray;
cvtColor(image,gray,CV_RGB2GRAY);
GaussianBlur( gray, gray, Size( 5, 5 ), 0, 0 );
vector<string> codes;
Mat corners;
findDataMatrix(gray, codes, corners);
drawDataMatrixCodes(image, codes, corners);
//Mat image = imread("");
//Rect region_of_interest = Rect(x, y, w, h);
//Mat image_roi = image(region_of_interest);
std::cout << image.cols << "\n";
std::cout << image.rows << "\n";
Rect roi(0,290,640,190);// set the ROI for the image
Mat imgROI = image(roi);
// Display the image
imwrite("original.bmp", imgROI);
// Canny algorithm
Mat contours;
Canny(imgROI, contours, 120, 300, 3);
imwrite("canny.bmp", contours);
Mat contoursInv;
threshold(contours,contoursInv,128,255,THRESH_BINARY_INV);
// Display Canny image
imwrite("contours.bmp", contoursInv);
/*
Hough tranform for line detection with feedback
Increase by 25 for the next frame if we found some lines.
This is so we don't miss other lines that may crop up in the next frame
but at the same time we don't want to start the feed back loop from scratch.
*/
std::vector<Vec2f> lines;
if (houghVote < 1 or lines.size() > 2){ // we lost all lines. reset
houghVote = 200;
}else{
houghVote += 25;
}
while(lines.size() < 5 && houghVote > 0){
HoughLines(contours,lines,1,PI/180, houghVote);
houghVote -= 5;
}
std::cout << houghVote << "\n";
Mat result(imgROI.size(),CV_8U,Scalar(255));
imgROI.copyTo(result);
// Draw the limes
std::vector<Vec2f>::const_iterator it= lines.begin();
Mat hough(imgROI.size(),CV_8U,Scalar(0));
while (it!=lines.end()) {
float rho= (*it)[0]; // first element is distance rho
float theta= (*it)[1]; // second element is angle theta
if ( theta > 0.09 && theta < 1.48 || theta < 3.14 && theta > 1.66 ) {
// filter to remove vertical and horizontal lines
// point of intersection of the line with first row
Point pt1(rho/cos(theta),0);
// point of intersection of the line with last row
Point pt2((rho-result.rows*sin(theta))/cos(theta),result.rows);
// draw a white line
line( result, pt1, pt2, Scalar(255), 8);
line( hough, pt1, pt2, Scalar(255), 8);
}
++it;
}
// Display the detected line image
std::cout << "line image:"<< "\n";
namedWindow("Detected Lines with Hough");
imwrite("hough.bmp", result);
// Create LineFinder instance
LineFinder ld;
// Set probabilistic Hough parameters
ld.setLineLengthAndGap(60,10);
ld.setMinVote(4);
// Detect lines
std::vector<Vec4i> li= ld.findLines(contours);
Mat houghP(imgROI.size(),CV_8U,Scalar(0));
ld.setShift(0);
ld.drawDetectedLines(houghP);
std::cout << "First Hough" << "\n";
imwrite("houghP.bmp", houghP);
// bitwise AND of the two hough images
bitwise_and(houghP,hough,houghP);
Mat houghPinv(imgROI.size(),CV_8U,Scalar(0));
Mat dst(imgROI.size(),CV_8U,Scalar(0));
threshold(houghP,houghPinv,150,255,THRESH_BINARY_INV); // threshold and invert to black lines
namedWindow("Detected Lines with Bitwise");
imshow("Detected Lines with Bitwise", houghPinv);
Canny(houghPinv,contours,100,350);
li= ld.findLines(contours);
// Display Canny image
imwrite("contours.bmp", contoursInv);
// Set probabilistic Hough parameters
ld.setLineLengthAndGap(5,2);
ld.setMinVote(1);
ld.setShift(image.cols/3);
ld.drawDetectedLines(image);
std::stringstream stream;
stream << "Lines Segments: " << lines.size();
putText(image, stream.str(), Point(10,image.rows-10), 2, 0.8, Scalar(0,0,255),0);
imwrite("processed.bmp", image);
char key = (char) waitKey(10);
lines.clear();
}
The following are the input images respectively:
Here I show two photos one that recognizes the white line and another that does not recognize the yellow line, what I require is to recognize the dividing lines because I monitor the lane, but is complicated to me and it does not recognize the presence of all dividing lines, I hope help me because I have honestly tried everything but I have not had good results.
I think it's because you are doing a bitwise addition of both probabilistic hough and regular hough transforms. This means that the outputted image will only contain lines that appear in both of these transforms. I'm pretty sure in the regular transform the line is not detected but in the probabilistic hough output the line is detected. You're best bet is to output both transforms separately and debug. I'm doing a similar project, I imagine you could include a separate ROI to exclude from the bitwise addition and that area would be along the centrum of the lane markings.

how to make hough line transform to execute faster?

i have written code for lane detection using, hough line transform,lines are identified in my video file stored in my pc [which is having 1280*720 resolution],my video is running slowly,how i can make run faster?,in my code i have checked the time of execution of function hough_transform comprising of canny,cvtcolor and hough transform,up on which i am retrieving the frames, i can able to execute two frames per/sec,please help me to reduce the execution time.thanks in advance
here is the code:
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int hough_tranform(Mat src){
if(src.empty())
{
cout << "can not open " << endl;
return -1;
}
Mat dst, cdst;
Canny(src, dst, 50, 200, 3);
cvtColor(dst, cdst, COLOR_GRAY2BGR);
vector<Vec4i> lines;
HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );
for( size_t i = 0; i < lines.size(); i++ )
{
Vec4i l = lines[i];
line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, 0);
}
imshow("detected lines", cdst);
}
int main() {
Mat frame;
string path = "C:/santhu/Wildlife.wmv";
VideoCapture capture(path);
namedWindow("my_window");
for(;;) {
capture >> frame;
hough_tranform(frame);
imshow("my_window", frame);
if(cv::waitKey(30) >= 0) break;
}
}
Playing around with the parameters of HoughLinesP function will help you to improve the performance a bit in the cost of precision. Performance will drastically reduce for this function when the image size increases.
If possible, use HoughLines instead of probabilistic approach as it is faster.
Downscaling the image using bilinear interpolation will not effect the quality of the output as hough transformation is carried out on canny edge detector output.
The steps that I would follow will be:
Read a frame.
Convert to grayScale.
Downscale the gray image.
If possible, select the ROI on the gray image on which lane is to be
detected.
Do canny on the ROI image.
Do hough transformation.
As you are doing lane detection algorithm I shall put my two cents in. Canny detection alone will not be of much help on road which contains shadows of trees etc as there will be edges detected around it. Though Probabilisitic Hough approach reduces the error in the above circumstances, (a) Limiting the theta value, (b) using sobel edge detection in which dx is given more priority than dy are some experiments worth trying.
You should downsize your image before performing edge detection, followed by Hough's Line Transform. Then you can upsize result back to the original size.

Drawing Bounding-Box using OpenCV in C++ Enviroment

Hello peeps I have developed a piece of software that draws contours of the input image, now I wont to take this to the next level and draw Bounding Box around objects of interest i.e. A person. I looked at boundingRect() function but i am struggling to understand it. Maybe there are different functions algorithms draw Bounding Box.....?
Here is the code of my program:
#include "iostream"
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<opencv\ml.h>
#include<opencv\cxcore.h>
#include <iostream>
#include <string>
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat)
#include <opencv2/highgui/highgui.hpp> // Video write
using namespace cv;
using namespace std;
Mat image; Mat image_gray; Mat image_gray2; Mat threshold_output;
int thresh=100, max_thresh=255;
int main(int argc, char** argv) {
//Load Image
image =imread("C:/Users/Tomazi/Pictures/Opencv/tomazi.bmp");
//Convert Image to gray & blur it
cvtColor( image,
image_gray,
CV_BGR2GRAY );
blur( image_gray,
image_gray2,
Size(3,3) );
//Threshold Gray&Blur Image
threshold(image_gray2,
threshold_output,
thresh,
max_thresh,
THRESH_BINARY);
//2D Container
vector<vector<Point>> contours;
//Fnd Countours Points, (Imput Image, Storage, Mode1, Mode2, Offset??)
findContours(threshold_output,
contours, // a vector of contours
CV_RETR_EXTERNAL,// retrieve the external contours
CV_CHAIN_APPROX_NONE,
Point(0, 0)); // all pixels of each contours
// Draw black contours on a white image
Mat result(threshold_output.size(),CV_8U,Scalar(255));
drawContours(result,contours,
-1, // draw all contours
Scalar(0), // in black
2); // with a thickness of 2
//Create Window
char* DisplayWindow = "Source";
namedWindow(DisplayWindow, CV_WINDOW_AUTOSIZE);
imshow(DisplayWindow, result);
waitKey(5000);
return 1;
}
Can anyone suggest an solution...? Perhaps direct me to some sources, tutorials etc. Reading OpenCV documentation and looking at the boundingRect() function i still dont understand. HELP PLEASE :)
But you can also easily compute the bounding box yourself and then draw them using the rectangle function:
int maxX = 0, minX = image.cols, maxY=0, minY = image.rows;
for(int i=0; i<contours.size(); i++)
for(int j=0; j<contours[i].size(); j++)
{
Point p = contours[i][j];
maxX = max(maxX, p.x);
minX = min(minX, p.x);
maxY = max(maxY, p.y);
minY = min(minY, p.y);
}
rectangle( result, Point(minX,minY), Point(maxX, maxY), Scalar(0) );
This link was not helpful?
I think it demonstrates how to take the contour object and make it a polygon approximation, plus how to draw the bounding rectangle around it.
It seems to be one of the basic OpenCV demos.
I've talked about the bounding box technique in these posts:
How to detect Text Area from image?
Contours opencv : How to eliminate small contours in a binary image
OpenCv 2.3 C - How to isolate object inside image (simple C++ demo)
I think that the last one can probably help you understand how the standard technique works. What OpenCV offers is an easier approach.