2017-04-23 2 views
0

이미지의 검정색 선 내에 포함 된 영역을 찾으려고합니다.채우기 영역은 검은 색 선으로 표시됩니다.

다음은 샘플 이미지를 시작하는 "photo.jpg를"입니다 :이 위해 OpenCV의 및 SimpleCV을 사용했다

Sample starting image "photo.jpg"

. 여기

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 

면적 계산에 사용되는 이미지 "mask.jpg"이다 : 여기서

코드이다

Generated image "mask.jpg"

가 관찰 : 내부 1. 흑색 패치 "mask.jpg"의 흰색 영역 2. "TAXI"라는 단어가있는 왼쪽 하단 구석의 흰색 부분

어떻게 제거합니까? 난 그냥 검은 선 안의 모든 것을 다 먹어보고 선 밖의 모든 것이 그 지역을 계산할 때 고려되지 않기를 바란다.

답변

0

나는 당신이 당신의 솔루션을 복잡하게 만들고 있다고 생각한다. 나는 당신의 코드를 수정하고 검은 경계 내에서 영역을 얻으려고했다. 영역이 맞는지 확실하지 않지만 미세 조정 방법을 제공합니다.

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) 

나는이에서 가져온 결과는 내가 생성 윤곽 (최대 윤곽)

enter image description here

는 희망이 도움으로 이미지를 마스킹 한 후이 이미지 결과를 얻었다

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

입니다 ! 행운을 빌어 요.

관련 문제