0

I am trying to find area contained within a black line in an image.

Here is the Sample starting image "photo.jpg":

Sample starting image "photo.jpg"

I have used OpenCV and SimpleCV for this.

Here is the code:

from SimpleCV import Camera, Display, Image, Color
import time
import cv2
import numpy as np

n_image = Image('photo.jpg')
n_image2 = n_image.crop(55, 72, 546, 276)  #Crop X,Y,W,H
n_image2.save('photo_2.jpg')
imagea = Image("photo_2.jpg")

greya = imagea.stretch(50).invert()  #50=Blackness level of Black
greya.show()
greya.save('photo_2-GREY.jpg')

im = cv2.imread('photo_2-GREY.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

ret,thresh = cv2.threshold(imgray,220,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
largest_areas = sorted(contours, key=cv2.contourArea)
cv2.drawContours(im, [largest_areas[-2]], 0, (255,255,255,255), -1)

cv2.drawContours(im,contours,-1,(255,255,255),-1)
cv2.imshow('Image Window',im)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('photo_3.jpg',im)

n_image = Image('photo_3.jpg')
mask = n_image.colorDistance((127, 127, 127))

mask.show()
mask.save('mask.jpg')
time.sleep(3)

binarised = mask.binarize()
blobs = binarised.findBlobs()
blobs.show(width=3)
time.sleep(60)

individualareaofholes = blobs.area()
compositeareaofholes = sum(individualareaofholes)
orig_area = 132432
finalarea = (orig_area - compositeareaofholes)
res = round(((finalarea/orig_area)*100),0)

print "Area is %d" % res

Here is the image "mask.jpg" which is used for area calculation:

Generated image "mask.jpg"

Observe: 1. the black patches inside the white area in "mask.jpg" 2. the white portion in the bottom left corner with the word "TAXI"

How do I eliminate them? I just want everything enclosed within the black line to be gobbled up and everything outside the line not to be accounted for while calculating the area.

Sjy
  • 1
  • 3

1 Answers1

0

I think you are complicating your solution(I may be wrong). I tried to modify your code and get the area within the black boundary. Not sure if the area is correct but it will get you a way to fine-tune it.

import cv2
import numpy as np

n_image = cv2.imread('5GZ6X.jpg') # Your original image

imgray =  cv2.cvtColor(n_image,cv2.COLOR_BGR2GRAY)
im_new = np.zeros_like(imgray)
ret,thresh = cv2.threshold(imgray,10,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
largest_areas = sorted(contours, key=cv2.contourArea)
cv2.drawContours(im_new, [largest_areas[-2]], 0, (255,255,255,255), -1)

image_masked = cv2.bitwise_and(imgray, imgray, mask=im_new)

area = cv2.contourArea(largest_areas[-2])

for contour in largest_areas:
    areas = cv2.contourArea(contour)
    if areas > 300:
        print areas

print 'Complete area :' + str(n_image.shape[0] * n_image.shape[1])

print 'Area of selected region : ' + str(area)

cv2.imshow('main', image_masked)
cv2.waitKey(1000)

The result I got from this is

113455.5
135587.0
303849.0
Complete area :307200
Area of selected region : 135587.0

I got this image result after masking the image with the contour generated(largest contour)

enter image description here

Hope this helps! good luck :)

harshkn
  • 731
  • 6
  • 13