Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.pratchaya.cv.imgproc;
- import com.googlecode.javacpp.Loader;
- import static com.googlecode.javacv.cpp.opencv_core.*;
- import static com.googlecode.javacv.cpp.opencv_imgproc.*;
- import static com.googlecode.javacv.cpp.opencv_highgui.*;
- import java.nio.ByteBuffer;
- /**
- *
- * @author BMCF
- */
- public class VerticalRotate {
- int[] max = new int[2];
- int[] min = new int[2];
- public IplImage apply(IplImage _image, CvRect _r) {
- CvRect r = _r; // อันนี้แค่ตำแหน่งข้าวจากรูปใหญ่ ไม่สำคัญ
- IplImage rice = cvCloneImage(_image); // สร้างรูปเอามาเก็บไว้ในตัวแปรรเหมือน buffer
- //find horizental
- if (rice.width() > rice.height()) {
- CvMat mat = new CvMat(); // เป็น matrix ใน opencv มันมีฟังก์ชันสรา้งให้เรา
- cvGetMat(rice, mat, null, 1); // เอารูปแปลงเป็นตัวเลข ลง matrix เรื่องทั่วไป
- IplImage trans = cvCreateImage(cvSize(mat.rows(), mat.cols()), rice.depth(), rice.nChannels()); // สรา้งรูปเปล่าๆ ทั่ว
- cvTranspose(rice, trans); // ไข่ว
- cvFlip(trans, trans, 1); // ต้องใช้ต่อจาก cvTranspose เสมอไม่มีไร
- rice = null;
- rice = cvCreateImage(cvSize(trans.width(), trans.height()), trans.depth(), trans.nChannels());
- rice = cvCloneImage(trans); // อันนี้เอารุปที่ไขว่ มาเก็บไว้ใช่ต่อ สองบรรทัดบนไม่มีไร
- }
- //set center object
- CvPoint2D32f center = new CvPoint2D32f(rice.width() / 2.0f, rice.height() / 2.0f); // หาจุดกลางภาพไว้สรา้งเป็นจุดหมุน
- int x = 0, y; // เก็บค่า x , y
- float m = (float) rice.height() / (float) rice.width(); // คนที่สอนผมบอกหาความชั่น
- float m1 = -1 * m; // อันนี้ผมไม่รู้ทำไร
- int fromleft = 0, fromright = 0; //อันนี้เอาไว้เก็บค่าว่าข้าวมันเอียงไปทางไหน
- ByteBuffer buffer = rice.getByteBuffer(); // เอารูปแปลงไปเป้ฯ byte เฉยๆ
- CvMat mat = new CvMat();// เป็น matrix ใน opencv มันมีฟังก์ชันสรา้งให้เรา
- cvGetMat(rice, mat, null, 1); // เอารูปแปลงเป็นตัวเลข ลง matrix เรื่องทั่วไป
- // วนตามความสูง เพื่อหาว่ามันเอียงไหน ถ้าเจอ ให้ fromleft++; เพื่อบอกว่าเอียงซ้าย น่าจะวนจากบนลงล่าง หาข้อมูลจากซ้ายไปขวา
- while (x < mat.cols()) { // mat.cols = column
- y = (int) (m * x);
- int index = x * rice.widthStep() + y; // อันนี้อะเป็นการส้ราง index สำหรับดึงรุป px ออกมา java ใช้การดึง matrx แบบมิติเดียว คือค่าเดียวครับ rice.widthStep() ก็คือความยาวของรูป มันมีสองแบบ rice.width()
- int value = buffer.get(index) & 0xFF; // ดึงออกมามาร์ก
- if (value == 255) {
- fromleft++;
- }
- x++;
- }
- // ตรงนี้จะ error ถ้าไม่ cvTranspose(rice, trans) จะ error ตรง int value = buffer.get(index) & 0xFF; อันนี้กเหมือนอันบนครับ อันนี้น่าจวนจากล่างขึ้น บนหาข้อมูลจาก ขวาซ้าย
- x = mat.cols(); //
- while (x > 0) {
- y = (int) m1 * x + mat.rows();
- int index = y * rice.widthStep() + x;
- int value = buffer.get(index) & 0xFF;
- if (value == 255) {
- fromright++;
- }
- x--;
- }
- double angle = Math.atan((double) mat.cols() / (double) mat.rows()) * 180 / 3.1415926535; // อันนี้ส่รางองศาหมุมจาก column / row
- rice = rotationMatrix(rice, fromleft, fromright, center, angle); // อันนี้ฟังก์ชันหมุนครับ รอบแรก ตัวฟังก์ชันอยู่ด่านล่างผมทำเป็น method ก็จะมีโยน รูป ความเอียง ซ้าย ขวา จุดกลางภาพ องศา
- // อันนี้ที่ผมคิดรุปสามเหลี่ยมวันนั้น หาค่า x ,y - top , buttom
- max = FindABContext.MaxContext(rice);
- min = FindABContext.minContext(rice);
- angle = Math.atan((double) (min[1] - max[1]) / (min[0] - max[0])); // สรา้งองศา
- double ang = 90 - Math.toDegrees(angle); // เปลี่ยนเป็นองศา ที่พี่บอกอะ
- if (min[0] > max[0]) { // x2 > x1
- ang *= -1;
- }
- rice = rotationMatrix(rice, fromleft, fromright, center, ang); // หมุนรอบสอง ครับเก็บรายละเอียดให้มันตรงเป๊ะ
- return rice;
- }
- อันนี้ตัวหมุนผมทำแยกไว้
- public IplImage rotationMatrix(IplImage _image, int fromleft, int fromright, CvPoint2D32f center, double angle) {
- IplImage dest = cvCloneImage(_image);
- CvMat rot = cvCreateMat(2, 3, CV_32FC1); // อันนี้เป้นเงือนไขที่ต้องส้รางครับไม่มีไร เป้นรูปแบบของมัน เป้นการสรา้ง matrix
- if (fromleft >= fromright) { // ดูว่าเอียงไปทางไหนครับ ซ้ายหรือขวา
- cv2DRotationMatrix(center, -angle, 1.0, rot); // อันนี้เอาข้อมูลไปเก็บไว้ใน matrix ไม่มีไรเกี่ยวข้องกับข้าว
- } else {
- cv2DRotationMatrix(center, angle, 1.0, rot); // เหมือนกัน
- }
- cvWarpAffine(dest, dest, rot); // อันนี้สั่งหมุนครับ โดยเอา matrix ที่ทำไว้มาทาบเฉยๆ arg ตัวแรกคือรุปที่เอาทำ ตัวสองคือ output ตัวสามคือ matrix ที่เอามาทาบกับ arg ตัวแรก เพื่อให้ได้ตัวสอง output
- return dest;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement