Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 2088

Deskew Text with OpenCV and Python

$
0
0
Hello, i'm new with OpenCV and i want to deskew an image that have a skew text: ![image description](/upfiles/14743401695051453.jpg)
First i read the image in GrayScale and Binarize it, then i try to do [this](http://opencvpython.blogspot.com.ar/2012/06/contours-2-brotherhood.html): import cv2 import numpy as np img = cv2.imread('m20.jpg',0) ret,byw = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV) _, contours, hierarchy = cv2.findContours(byw.copy(), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) cnt = contours[0] draw = cv2.cvtColor(byw, cv2.COLOR_GRAY2BGR) rect = cv2.minAreaRect(cnt) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(draw, [box], 0, (0, 255, 0), 2) But doesn't work beacause findContours() expect to receive an image with a body shape. Other way that i try is to translate this code of c++: // Read image Mat3b img = imread("path_to_image"); // Binarize image. Text is white, background is black Mat1b bin; cvtColor(img, bin, COLOR_BGR2GRAY); bin = bin < 200; // Find all white pixels vector pts; findNonZero(bin, pts); // Get rotated rect of white pixels RotatedRect box = minAreaRect(pts); if (box.size.width > box.size.height) { swap(box.size.width, box.size.height); box.angle += 90.f; } Point2f vertices[4]; box.points(vertices); for (int i = 0; i < 4; ++i) { line(img, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0)); } // Rotate the image according to the found angle Mat1b rotated; Mat M = getRotationMatrix2D(box.center, box.angle, 1.0); warpAffine(bin, rotated, M, bin.size()); And i have this: draw = cv2.cvtColor(byw, cv2.COLOR_GRAY2BGR) data = np.array(byw) subzero = np.nonzero(data) subuno = np.reshape(subzero,(17345,2)) # this is because cv2.minAreaRect() receives a Nx2 numpy rect = cv2.minAreaRect(subuno) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(draw,[box],0,(0,255,0),2) But then again the result it's not the expected: ![image description](/upfiles/1474340414835570.jpg)
Also occurs to me that might try to make the 'for' like in C++ but i don't know how to obtain the vertices from the 'box = cv2.boxPoints(rect)'. Please help!

Viewing all articles
Browse latest Browse all 2088

Trending Articles