2017-04-05 6 views
0

이미지를 뒤틀기 opencv를 사용하여 수행하고 싶습니다. 이미지 (예 : 4000x4000)의 네 모서리를 감지했으며 opencv에서 을 사용하여 변환 행렬을 얻었습니다. 이제 이미지 센터에서 이미지를 왜곡하고 싶습니다. 그래서 warpPerspective을 사용했습니다. 입력 한 원본 이미지의 크기가 450x450이고 전체 이미지에 대해 계산 한 변환 행렬을 사용합니다.이미지 opencv를 사용하여 뒤틀림

하지만 아무 것도 화면에 표시되지 않습니다. 도움을 주시면 감사하겠습니다.

다음은 샘플 코드 및 샘플 이미지입니다.

이것은 원본 이미지입니다. This is source image.

이 이미지는 I 이미지에서 원하는 휨 영역. (코드 src_crop)

enter image description here

이 결과이다 This image is detected

를 검출한다.

enter image description here

 source[0] = Point2f(lt.x, lt.y); 
     source[1] = Point2f(rt.x, rt.y); 
     source[2] = Point2f(rb.x, rb.y); 
     source[3] = Point2f(lb.x, lb.y); 

     dst[0] = Point2f(6, 10); 
     dst[1] = Point2f(3899, 7); 
     dst[2] = Point2f(3901, 3899); 
     dst[3] = Point2f(9, 3902); 

     Mat transformMatrix ; 
     transformMatrix = getPerspectiveTransform(source, dst); 



     Mat dstFrame ; 
     warpPerspective(src_crop, dstFrame, transformMatrix, Size(450, 450)); 

답변

1

당신이 당신의 getPerspectiveTransform 방법에 대한 잘못된 값을 사용하기 때문에 귀하의 변환이 실패합니다. 출력 이미지를 만드는 방법과이 이미지의 데이터에서 대상 모서리를 채우는 방법을 혼합하는 것처럼 보입니다.

또한 배열의 오른쪽 모서리 (왼쪽 위, 오른쪽 위, 왼쪽 아래, 오른쪽 아래)를 연결하는 것이 중요합니다.

// Assuming your source image is called 'sourceImage' and you have the corner points you need: 

// Create vectors to store the corners 
vector<Point2f> originalCorners; 
vector<Point2f> destinationCorners; 

// Put the Sudoku corners in your originalCorners 
originalCorners.clear(); 
originalCorners.push_back(Point2f(lt.x, lt.y); 
originalCorners.push_back(Point2f(rt.x, rt.y); 
originalCorners.push_back(Point2f(lb.x, lb.y); 
originalCorners.push_back(Point2f(rb.x, rb.y); 

// Output image size of 450x450 
int ouputImageWidth = 450; 
int outputImageHeight = 450; 

// Create an empty image (450x450) to output your transformation result in 
Mat transformedOutputImage(ouputImageWidth, outputImageHeight, sourceImage.type()); 

// Now put the corners of the output image so the warp knows where to warp to 
destinationCorners.clear(); 
destinationCorners.push_back(Point2f(0, 0)); 
destinationCorners.push_back(Point2f(ouputImageWidth, 0)); 
destinationCorners.push_back(Point2f(0, outputImageHeight)); 
destinationCorners.push_back(Point2f(ouputImageWidth, outputImageHeight)); 

// Now we have all corners sorted, so we can create the warp matrix 
Mat warpMatrix = getPerspectiveTransform(originalCorners, destinationCorners); 

// And now we can warp the Sudoku in the new image 
warpPerspective(sourceImage, transformedOutputImage, warpMatrix, Size(ouputImageWidth, ouputImageHeight)); 

지금이 당신이 휘게 할 이미지 부분의 모서리 점을 알고 가정

이 예는 빈 출력 이미지에서 우측 지점과 출력을 연결하는 방법을 보여줍니다 . 중간 사각형의 포인트를 얻는 방법을 모른다면 these excellent answers을 살펴 보시기 바랍니다.

그러나 네 모서리 점이있는 한이 방법을 사용할 수 있습니다. 수동으로 중간 사각형 코너 점을보고 삽입하여 시도 할 수도 있습니다.

+0

답변 해 주셔서 감사합니다. 나는 전체 이미지 (4000 x 4000)를 계산하는 변환 행렬을 안다. 하지만 원본 이미지로 warpPerspective에 src_crop (450 by 450) 이미지를 입력했습니다. 그래서 src_crop 이미지가 변환 행렬을 사용하여 왜곡되어 있다고 생각합니다. 불가능하다? src_crop 이미지의 구석 점을 모르면 src_crop 이미지의 이미지를 왜곡하고 싶습니다 – eonjeo

+0

자르기 대신'sourceImage' (4000x4000)를 사용해야합니다. 자르기는 'originalCorners' 배열이 존재하지 않는 모퉁이를 가리키게합니다. 뒤틀림을하기 전에 부패한 적이 없지만 출력이 완전히 검은 색이 될 수 있습니다. – Jurjen

+0

실제로 프로그램의 실행 시간을 줄이기 위해 자르기 이미지를 사용합니다. 자르기 이미지의 중심점을 찾기 위해 matchTemplate 함수를 사용합니다. 그래서 전체 이미지 뒤틀림 대신에 자른 이미지 뒤틀림이 뒤틀림을위한 짧은 시간이라고 생각합니다. – eonjeo

관련 문제