Detecting location of translucent black rectangluar area in image matrix Python OpenCV
<p>Say I have an image like this:</p>

<p><a href="http://i.stack.imgur.com/G5AlE.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/G5AlE.jpg" alt="enter image description here"></a></p>

<p>I want the <strong>location of the start and end points of the black strip in the image matrix</strong>.</p>

<p>I have tried several method like <a href="http://stackoverflow.com/questions/7227074/horizontal-line-detection-with-opencv">horizontal line detection in Python OpenCV</a> and have come up with following code that gets me the lines highlighted:</p>

<pre><code>import cv2
import numpy as np
from numpy import array
from matplotlib import pyplot as plt
import math

img = cv2.imread('caption.jpg')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize = 3)
lines = cv2.HoughLinesP(edges, 1,np.pi/180,350);
for line in lines[0]:
    pt1 = (line[0],line[1])
    pt2 = (line[2],line[3])
    cv2.line(img, pt1, pt2, (0,0,255))



gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)

print img.shape

lines = cv2.HoughLines(edges,1,np.pi/180,350)

for rho,theta in lines[0]:
    a = np.cos(theta)
    b = np.sin(theta)
    if int(b) == 1: #only horizontal lines with cos theta(theta = 0) = 1
        x0 = a*rho
        y0 = b*rho
        x1 = int(x0 + 1000*(-b))
        y1 = int(y0 + 1000*(a))
        x2 = int(x0 - 1000*(-b))
        y2 = int(y0 - 1000*(a))

    cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)

cv2.imshow('edges', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
</code></pre>

<p>Result:
<a href="http://i.stack.imgur.com/QLKmE.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/QLKmE.jpg" alt="enter image description here"></a></p>

<p>If I try <code>print x1, y1, x2, y2</code> i get </p>

<blockquote>
  <p>-1000 781 999 782
  -1000 712 999 713</p>
</blockquote>

<p>So these obviously are not the location points in the image matrix like x is negative. </p>

<p><strong>What is the location of the start and endpoints of these lines in the image matrix?</strong> I need to perform some point operation on the pixels in that area and hence need the start and endpoints.</p>

<p>These lines will always return -1000 + the original point</p>

<pre><code>x1 = int(x0 + 1000*(-b))
x2 = int(x0 - 1000*(-b))
</code></pre>

<p>As you only get into this loop if <code>int(b) == 1:</code></p>

<p>Which means that you need to print the x0 directly, since the above lines will always be <code>(x0 + (-1000))</code>, <strong>in this case x0 is 0 since it starts from the left of the image.</strong></p>

python opencv image-processing opencv-contour