2014-01-16 3 views
1

좌표 목록을 좌표로 저장하여 여러 위치에서 자르기를 시도한 후 자른 부분의 문자가 매우 흐릿 해져서 왜 그럴 수 없는지 알 수 없습니다.자르기 기능 후 글자가 흐릿하게/흐리게 처리됩니다.

import numpy as np 
import cv2 

im2 = cv2.imread('1.jpg') 
im = im2.copy() 

gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 
blur = cv2.GaussianBlur(gray,(5,5),0) 
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2) 


contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) 


squares = [] 

for cnt in contours: 
    if cv2.contourArea(cnt)>50: 
     [x,y,w,h] = cv2.boundingRect(cnt) 

     if h>28 and h<34: 
      rect = (cv2.rectangle(im,(x,y),(x+w,y+h),(255,255,255),3)) 
      squares.append(cv2.boundingRect(cnt)) 
      cv2.imwrite('norm1.jpg',im) 

crop_img = [[[255, 255, 255] for x in xrange(377)] for x in xrange(377) ] 

for s in squares: 
    x = s[0] 
    y = s[1] 
    w = s[2] 
    h = s[3] 
    img = im[y:y+h,x:x+w] 
    for col in range(y,y+h): 
     for row in range(x,x+w): 
      if img[col - y][row - x].tolist() == [0,0,0]: 
       crop_img[col][row] = [0,0,0] 
cv2.imwrite("cropped.jpg", np.array(crop_img)) 

어떤 도움을 :

1

이미지를 자르기하는

2

질문의 코드는 다음과 같다 같습니다 후

처럼

원본 이미지가 보입니다 너 크게 감사드립니다!

+0

. 또한 확실하지 않지만 데이터 유형이 어딘가에서 변경되었는지 확인하십시오. –

+0

안녕하세요. "첫 번째 및 두 번째"로 무엇을 의미하는지 확신 할 수 없지만 내 데이터 유형이 "if img [col - y] [row - x] .tolist() == [0,0, 0] : "오류가 발생했기 때문에 .tolist 함수를 사용해야했습니다. ValueError : 두 개 이상의 요소가있는 배열의 진리 값이 모호합니다. a.any() 또는 a.all()을 사용하십시오. 이 내용 중 일부가 삭제되기를 바랍니다. 감사! – JamesLLee

+0

아 ... 죄송합니다. "귀하의 코드에서 어떤 변수가 질문의 첫 번째 이미지이고 어떤 변수가 두 번째 이미지인지 묻고 싶습니다." –

답변

1
import numpy as np 
import cv2 
import matplotlib.pyplot as plt 

im2 = cv2.imread('norm1_zps89266edb.jpg') 
im = im2.copy() 

gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 
blur = cv2.GaussianBlur(gray,(5,5),0) 
ret3,thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 

#we ony want the external contours 
contours,hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) 
#extract the countours with area > 50 
squares = [cnt for cnt in contours if cv2.contourArea(cnt) > 50] 

#mask array with the same shape as img (but only 1 channel) 
mask = np.zeros((im.shape[0], im.shape[1])) 
#draw the contours filled with 255 values. 
cv2.drawContours(mask,squares,-1,255,-1) 

newImage = np.where(mask==255, thresh, 255) 

plt.imshow(newImage) 
plt.show() 

cv2.imwrite("cropped.jpg", newImage) 

출력 : 변수가 첫 번째 및 두 번째하는 가변 코드에서는
http://i44.tinypic.com/8yak93.jpg

+0

끝내 줘! 정말 고마워 – JamesLLee

관련 문제