Main aim is to obtain the distance between the mast (as shown the original image) and the camera. The code works pretty well for detecting the pantry wires (since sky is the background) with the help of HoughLines. But when it comes for mast detection, since there are so many objects that can be the part of the background scene, I thought of blurring the original image, apply canny edge detection , do some dilation for the edges in the mast, then after combining them, I got an image where the edges of the mast are visible for getting the contours. But it is found that the contour is drawn around entire image border instead of the required mast (required image). I tried visualizing each of the steps as shown output.Can anyone help me out in building the logic if the way I thought is incorrect? Below is the copy of my code:
import cv2
import numpy as np
import imutils
from matplotlib import pyplot as plt
img = cv2.imread('images/Mast4.jpeg',0)
#image before blurring bkgd
edges1 = cv2.Canny(img,70,75)
gray = cv2.GaussianBlur(img, (15, 25), 0)
#get well-defined borders for the mast
kernel = np.ones((5,5), np.uint8)
img_dilation = cv2.dilate(gray, kernel, iterations=1)
#image after blurring bkgd
edges2 = cv2.Canny(img_dilation,70,75)
added = img + gray + edges2
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges1,cmap = 'gray')
plt.title('Normal edged Image'), plt.xticks([]), plt.yticks([])
plt.figure()
plt.subplot(121),plt.imshow(gray,cmap = 'gray')
plt.title('Blurred Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges2,cmap = 'gray')
plt.title('Edged Image'), plt.xticks([]), plt.yticks([])
plt.figure()
plt.subplot(121),plt.imshow(added,cmap = 'gray')
plt.title('added Image'), plt.xticks([]), plt.yticks([])
#draw contours/ bounding boxes
cnts = cv2.findContours(added, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key = cv2.contourArea)
marker = cv2.minAreaRect(c)
box = cv2.cv.BoxPoints(marker) if imutils.is_cv2() else cv2.boxPoints(marker)
box = np.int0(box)
cv2.drawContours(added, [box], -1, (0, 0, 255), 2)
plt.subplot(122),plt.imshow(added)
plt.title('final Image'), plt.xticks([]), plt.yticks([])
plt.show()