void HoughLines(InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn=0, double stn=0 )
Parameters:
-------------
if you want to detect vertical lines, use the following conditional statement after line 23 of the above example.
Parameters:
- image – 8-bit, single-channel binary source image (use edge detectors)
- lines – Output vector of lines. Each line is represented by a two-element vector . is the distance from the coordinate origin (top-left corner of the image). is the line rotation angle in radians ( ).
- rho – Distance resolution of the accumulator in pixels.
- theta – Angle resolution of the accumulator in radians.
- threshold – Accumulator threshold parameter. Only those lines are returned that get enough votes (>threshold ).
- srn – For the multi-scale Hough transform, it is a divisor for the distance resolution rho . The coarse accumulator distance resolution is rho and the accurate accumulator resolution is rho/srn . If both srn=0 and stn=0 , the classical Hough transform is used. Otherwise, both these parameters should be positive.
- stn – For the multi-scale Hough transform, it is a divisor for the distance resolution theta.
Steps:
- Load image and convert to gray-scale.
- Apply the Hough Transform to find the lines.(HoughLines)
- Draw the detected lines.(line)
- Show the result
Functions:
Example:
-------------#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace cv; using namespace std; int main() { Mat src = imread("building.jpg", 0); Mat dst, cdst; Canny(src, dst, 50, 200, 3); cvtColor(dst, cdst, CV_GRAY2BGR); vector<Vec2f> lines; // detect lines HoughLines(dst, lines, 1, CV_PI/180, 150, 0, 0 ); // draw lines for( size_t i = 0; i < lines.size(); i++ ) { float rho = lines[i][0], theta = lines[i][1]; Point pt1, pt2; double a = cos(theta), b = sin(theta); double x0 = a*rho, y0 = b*rho; pt1.x = cvRound(x0 + 1000*(-b)); pt1.y = cvRound(y0 + 1000*(a)); pt2.x = cvRound(x0 - 1000*(-b)); pt2.y = cvRound(y0 - 1000*(a)); line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA); } imshow("source", src); imshow("detected lines", cdst); waitKey(); return 0; }
-------------
Result:
Extra Stuffs:
- What if you need to select some lines on the basis of prior knowledge of range of angles?
if you want to detect vertical lines, use the following conditional statement after line 23 of the above example.
if( theta>CV_PI/180*170 || theta<CV_PI/180*10) { Point pt1, pt2; .......... line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA); }if you want to detect horizontal lines, use
if( theta>CV_PI/180*80 && theta<CV_PI/180*100) { Point pt1, pt2; .......... line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA); }
If i know that the lines im trying to detect will be in a specific range, e.g between 80 and 110 degrees, can I limit the range that houghLines searches through?
ReplyDeleteExcellent question Borg.
DeleteI have updated the post. Please have a look.
All you need to do is restrict your angle range. i.e. choose the lines those fall in your angle range. This can be done using a conditional statement after line detection.
Hi Kanha,
DeleteHope you can teach on the specific degree. What I had understand is totally weird
?
1
2
3
4
5
if( theta>CV_PI/180*170 || theta 170 degree || means OR in C++ .. and still confusing.. please help to details out then.. appreciate on yr response
zamani
Here "theta" is in radian. So we have to convert degree to radian.
DeleteHere 170 degree = (CV_PI/180)*170 radian.
We may not get perfect horizontal lines. So we have to put some threshold. Perfect horizontal line occurs when theta = CV_PI/2 = 90 degree. So we use the threshold from 80~100 degrees to detect horizontal lines.
For vertical lines, the angle is 0 degree = 180 degree.
So we can write that if (angle<10 degree OR angle>170 degree), then it is a vertical line.
Hope this helps.
Hi Kanha,
DeleteThanks for the details up and I had tested again and yes it worked as per define above. TQSM!!
Just another question related to Hough Transform but for Probabilistic Hough Line Transform. In this function, the HT Prob
HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );
with the arguments:
◾dst: Output of the edge detector. It should be a grayscale image (although in fact it is a binary one)
◾lines: A vector that will store the parameters (x_{start}, y_{start}, x_{end}, y_{end}) of the detected lines
◾rho : The resolution of the parameter r in pixels. We use 1 pixel.
◾theta: The resolution of the parameter \theta in radians. We use 1 degree (CV_PI/180)
◾threshold: The minimum number of intersections to “detect” a line
◾minLinLength: The minimum number of points that can form a line. Lines with less than this number of points are disregarded.
◾maxLineGap: The maximum gap between two points to be considered in the same line.
Here can the rho and theta been employed again to get the selective degree? The lines now are vectors X start, Y start, X end and Y end which is different.
can I limit the range that houghLines probablistic searches through? If yes, should I declare again using x start, y start, x end and y end?
Thanks
Zamani
Hi Kanha
ReplyDeleteif replace vector lines; by vector lines; then what are define rho and theta?
Vec2f by Vec4i
DeleteThese two datatypes are defined as:
Deletetypedef Vec Vec2f;
typedef Vec Vec4i;
So, vec2f can contain two float values. Similarly, vec4i can have four integer values.
However, we need two float values to represent a line, i.e. slope and distance from origin.
Therefore, vec2f is used.
Thanks for you reply, Kanha.
ReplyDeleteIn my case, i used Vec4i and i want to detect vertical lines, but i don't know how to defined rho and theta.
This is my code
vector< Vec4i > lines; //don't space between < and Vec4i
int houghThreshold = 70;
if(imgGRAY.cols*imgGRAY.rows < 400*400)
houghThreshold = 100;
cv::HoughLinesP(imgCanny, lines, 1, CV_PI/180, houghThreshold, 10,10);
while(lines.size() > MAX_NUM_LINES)
{
lines.clear();
houghThreshold += 10;
cv::HoughLinesP(imgCanny, lines, 1, CV_PI/180, houghThreshold, 10, 10);
}
for(size_t i=0; i<lines.size(); i++)
{
Point pt1, pt2;
//defined rho and theta to detect vertical lines ???
pt1.x = lines[i][0];
pt1.y = lines[i][1];
pt2.x = lines[i][2];
pt2.y = lines[i][3];
line(outputImg, pt1, pt2, CV_RGB(0,0,0), 2);
aux.clear();
aux.push_back(pt1);
aux.push_back(pt2);
lineSegments.push_back(aux);
}
Could you help me with this?
thanks allot brother, thanks allot!! :)
ReplyDeletecan you explain about those line :
ReplyDeletept1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
Why a and b must be multiplicated by 1000?
Very good! It was very usefull to me. Thank you
ReplyDeleteIs there any way to limit minimal length of a line?
ReplyDeleteThanks for great article
Hi Kanha, your tutorial is very helpful. I use HoughLinesP and I want to merge or average neigbour lines. Do you have any suggestion? Thanks
ReplyDeleteHi Kanha,
ReplyDeletehow can we measure the distance between two lines or how to get the desired line?
in my case i want to detect the tube and width of tube is 46 pixel, so i just want to draw line which has distance 46 between them and rest line should be neglect.
Any suggestion.
Thank you for tutorial.
Hello there i am new in opencv as well as in image processing
ReplyDeletei want to detect the crack in biscuit so i used the haugh transform as above example so i can crack this.
Please tell me how to do this.
Here i am processing
1->take an image
2->adaptive segment
3->haugh line transform using above code.
but line is not detected.
red window displays.
what is the role of threshold here? canyou explain
ReplyDeletepython training in bangalore | python online training
ReplyDeleteaws training in Bangalore | aws online training
artificial intelligence training in bangalore | artificial intelligence online training
machine learning training in bangalore | machine learning inlone training
data science training in bangalore | data science online training
aşk kitapları
ReplyDeleteyoutube abone satın al
cami avizesi
cami avizeleri
avize cami
no deposit bonus forex 2021
takipçi satın al
takipçi satın al
takipçi satın al
takipcialdim.com/tiktok-takipci-satin-al/
instagram beğeni satın al
instagram beğeni satın al
btcturk
tiktok izlenme satın al
sms onay
youtube izlenme satın al
no deposit bonus forex 2021
tiktok jeton hilesi
tiktok beğeni satın al
binance
takipçi satın al
uc satın al
sms onay
sms onay
tiktok takipçi satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
instagram beğeni satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
takipcialdim.com/instagram-begeni-satin-al/
perde modelleri
instagram takipçi satın al
instagram takipçi satın al
takipçi satın al
instagram takipçi satın al
betboo
marsbahis
sultanbet
This blog iswhat im exactly looking for. Great! and Thanks to you. 토토
ReplyDeleteWhat an interesting article! I'm glad i finally found what i was looking for.
ReplyDelete온라인경마
경마사이트
Sitemiz ile kazançlı bet oynabilirsiniz. Venüsbet Venüsbet Grandbetting Grandbetting Galabet Meritking Asyabahis Aresbet Maltcasino Grandbetting
ReplyDeletetiktok jeton hilesi
ReplyDeletetiktok jeton hilesi
binance referans kimliği
gate güvenilir mi
tiktok jeton hilesi
paribu
btcturk
bitcoin nasıl alınır
ReplyDeleteHello friends, did you know, tourist visa to India can be used for tourism, visiting family and friends or attending yoga retreats. Learn more about the e-Tourist visa for India.
I really hope you all are doing well and enjoying life to the fullest. I thank you so much for your unmistakable generosity in sharing this kind of invaluable information. To start international travel, understand the different types of Visa. Tourist visa facilitates leisure travel, while business visa facilitates business activities. Student visas are for educational pursuits, and work visas provide employment abroad. Temporary visas cover short-term stays, while permanent visas allow for extended stays. Understanding these visa types helps travelers meet entry requirements and plan their travels with confidence, ensuring a seamless experience across borders.
ReplyDeleteWow! I'm usually not into blog posts, but this write-up captivated me! Your unique writing style pleasantly surprised me. Excellent job! Keep it up! Feel free to explore more of our content. Facts About Turkey:
ReplyDelete1. Unique transcontinental location
2. Home of ancient Troy
3. Rich cultural heritage
4. Mouthwatering cuisine
5. Stunning landscapes
salt likit
ReplyDeletesalt likit
VBVHG
erzincan
ReplyDeletevan
antep
elazığ
urfa
KGRQS
I have to say your blog is a game-changer. The clarity of your writing and the depth of your knowledge are awe-inspiring. Keep up the fantastic work . Do you know The Belgium Visa in Kenya travelers streamlines the application process, making it more convenient and efficient. This digital system simplifies the Belgium Visa in Kenya procedure, offering a quicker and more accessible way for Kenyan tourists to explore the captivating beauty and cultural heritage of Belgium.
ReplyDeleteI'm truly grateful for your generosity in sharing these with us. I'm committed to taking part in the upcoming event, as this topic holds a special appeal for me. There's important information to share; the provided link has all the details. Egypt Residency and Work Authorization. Navigate the process of obtaining residency and work authorization smoothly. Whether you're seeking a job or looking to establish roots in this historically rich nation, understanding the regulations.
ReplyDelete"Within your article, we undertake a captivating exploration through the ever-shifting landscape of technology. Your knack for simplifying intricate concepts is genuinely remarkable, making it inclusive for all. Navigating your work feels like an enlightening journey. Gratitude for sharing this enriching piece."
ReplyDeleteشركة تنظيف مكيفات بجدة
ReplyDeleteمكيفات
39046
ReplyDeleteSilivri Parke Ustası
Tekirdağ Çatı Ustası
steroid cycles
Van Evden Eve Nakliyat
buy sustanon
masteron for sale
order dianabol methandienone
buy clenbuterol
anapolon oxymetholone
Immerse yourself in a literary odyssey as this blog post adeptly navigates the intricacies of its subject. The author's insightful perspective and eloquent prose form a captivating narrative that transcends the mundane. Beyond a mere article, it's an enriching journey that captivates the mind and soul. Dive into this exploration of profound insights and compelling storytelling.
ReplyDeleteF051F
ReplyDeleteKripto Para Kazanma
Madencilik Nedir
Bitcoin Nasıl Kazılır
Bitcoin Nasıl Kazanılır
Coin Kazanma Siteleri
Okex Borsası Güvenilir mi
Ön Satış Coin Nasıl Alınır
Yeni Çıkacak Coin Nasıl Alınır
Kripto Para Kazanma Siteleri
Your blog post enraptured me with its innovative insights and eloquent delivery. Your adept analysis showcased profound intellect and comprehensive comprehension. I wholeheartedly agreed with your perspective, contemplating your stimulating ideas. Turkey visa from India the Turkey Visa provides a convenient way for Indian citizens to obtain travel authorization for visiting Turkey. With a straightforward online application process, travelers can secure their eVisa swiftly for tourism, business, or transit purposes. This streamlined system facilitates easy entry into Turkey, enhancing travel efficiency for Indians.
ReplyDeleteThe dedication to excellence displayed on your website, along with the abundance of information it offers, is truly noteworthy. Stumbling upon a blog that presents original viewpoints rather than redundant content is a welcomed revelation!
ReplyDeleteThank you for providing such valuable content! Your guide was perfect, and 123 hp com setup filled in any extra details I needed.
ReplyDelete