Advertisement
Avatarr

ro

Jul 6th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.43 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package com.pratchaya.cv.imgproc;
  6.  
  7. import com.googlecode.javacpp.Loader;
  8. import static com.googlecode.javacv.cpp.opencv_core.*;
  9. import static com.googlecode.javacv.cpp.opencv_imgproc.*;
  10. import static com.googlecode.javacv.cpp.opencv_highgui.*;
  11. import java.nio.ByteBuffer;
  12.  
  13. /**
  14.  *
  15.  * @author BMCF
  16.  */
  17. public class VerticalRotate {
  18.  
  19.     int[] max = new int[2];
  20.     int[] min = new int[2];
  21.  
  22.     public IplImage apply(IplImage _image, CvRect _r) {
  23.  
  24.         CvRect r = _r; // อันนี้แค่ตำแหน่งข้าวจากรูปใหญ่ ไม่สำคัญ
  25.         IplImage rice = cvCloneImage(_image); // สร้างรูปเอามาเก็บไว้ในตัวแปรรเหมือน buffer
  26.         //find horizental
  27.         if (rice.width() > rice.height()) {
  28.             CvMat mat = new CvMat(); // เป็น matrix ใน opencv มันมีฟังก์ชันสรา้งให้เรา
  29.             cvGetMat(rice, mat, null, 1); // เอารูปแปลงเป็นตัวเลข ลง matrix เรื่องทั่วไป
  30.             IplImage trans = cvCreateImage(cvSize(mat.rows(), mat.cols()), rice.depth(), rice.nChannels()); // สรา้งรูปเปล่าๆ ทั่ว
  31.             cvTranspose(rice, trans); // ไข่ว
  32.             cvFlip(trans, trans, 1); // ต้องใช้ต่อจาก cvTranspose เสมอไม่มีไร
  33.             rice = null;
  34.             rice = cvCreateImage(cvSize(trans.width(), trans.height()), trans.depth(), trans.nChannels());
  35.             rice = cvCloneImage(trans); // อันนี้เอารุปที่ไขว่ มาเก็บไว้ใช่ต่อ สองบรรทัดบนไม่มีไร
  36.         }
  37.  
  38.         //set center object
  39.         CvPoint2D32f center = new CvPoint2D32f(rice.width() / 2.0f, rice.height() / 2.0f); // หาจุดกลางภาพไว้สรา้งเป็นจุดหมุน
  40.         int x = 0, y; // เก็บค่า x , y
  41.         float m = (float) rice.height() / (float) rice.width(); // คนที่สอนผมบอกหาความชั่น
  42.         float  m1 = -1 * m; // อันนี้ผมไม่รู้ทำไร
  43.         int fromleft = 0, fromright = 0; //อันนี้เอาไว้เก็บค่าว่าข้าวมันเอียงไปทางไหน
  44.         ByteBuffer buffer = rice.getByteBuffer(); // เอารูปแปลงไปเป้ฯ byte เฉยๆ
  45.  
  46.         CvMat mat = new CvMat();// เป็น matrix ใน opencv มันมีฟังก์ชันสรา้งให้เรา
  47.         cvGetMat(rice, mat, null, 1);  // เอารูปแปลงเป็นตัวเลข ลง matrix เรื่องทั่วไป
  48.  
  49. // วนตามความสูง เพื่อหาว่ามันเอียงไหน ถ้าเจอ ให้  fromleft++; เพื่อบอกว่าเอียงซ้าย น่าจะวนจากบนลงล่าง หาข้อมูลจากซ้ายไปขวา
  50.         while (x < mat.cols()) { // mat.cols = column
  51.             y = (int) (m * x);
  52.  
  53.             int index = x * rice.widthStep() + y; // อันนี้อะเป็นการส้ราง index สำหรับดึงรุป px ออกมา java ใช้การดึง matrx แบบมิติเดียว คือค่าเดียวครับ  rice.widthStep() ก็คือความยาวของรูป มันมีสองแบบ rice.width()
  54.             int value = buffer.get(index) & 0xFF; // ดึงออกมามาร์ก
  55.             if (value == 255) {
  56.                 fromleft++;
  57.             }
  58.             x++;
  59.         }
  60.  
  61. // ตรงนี้จะ error ถ้าไม่   cvTranspose(rice, trans) จะ error ตรง int value = buffer.get(index) & 0xFF; อันนี้กเหมือนอันบนครับ อันนี้น่าจวนจากล่างขึ้น บนหาข้อมูลจาก ขวาซ้าย
  62.         x = mat.cols(); //
  63.         while (x > 0) {
  64.             y = (int) m1 * x + mat.rows();
  65.  
  66.             int index = y * rice.widthStep() + x;
  67.             int value = buffer.get(index) & 0xFF;
  68.             if (value == 255) {
  69.                 fromright++;
  70.             }
  71.             x--;
  72.         }
  73.  
  74.        
  75.         double angle = Math.atan((double) mat.cols() / (double) mat.rows()) * 180 / 3.1415926535; // อันนี้ส่รางองศาหมุมจาก column / row
  76.  
  77.         rice = rotationMatrix(rice, fromleft, fromright, center, angle); // อันนี้ฟังก์ชันหมุนครับ รอบแรก ตัวฟังก์ชันอยู่ด่านล่างผมทำเป็น method ก็จะมีโยน รูป ความเอียง ซ้าย ขวา  จุดกลางภาพ องศา
  78.  
  79.  
  80.        // อันนี้ที่ผมคิดรุปสามเหลี่ยมวันนั้น หาค่า x ,y - top , buttom
  81.         max = FindABContext.MaxContext(rice);
  82.         min = FindABContext.minContext(rice);
  83.  
  84.        angle = Math.atan((double) (min[1] - max[1]) / (min[0] - max[0])); // สรา้งองศา
  85.         double ang = 90 - Math.toDegrees(angle); // เปลี่ยนเป็นองศา ที่พี่บอกอะ
  86.         if (min[0] > max[0]) { // x2 > x1
  87.             ang *= -1;
  88.         }
  89.  
  90.         rice = rotationMatrix(rice, fromleft, fromright, center, ang); // หมุนรอบสอง ครับเก็บรายละเอียดให้มันตรงเป๊ะ
  91.      
  92.  
  93.         return rice;
  94.  
  95.     }
  96.  
  97.  
  98.  
  99. อันนี้ตัวหมุนผมทำแยกไว้
  100.     public IplImage rotationMatrix(IplImage _image, int fromleft, int fromright, CvPoint2D32f center, double angle) {
  101.  
  102.         IplImage dest = cvCloneImage(_image);
  103.         CvMat rot = cvCreateMat(2, 3, CV_32FC1); // อันนี้เป้นเงือนไขที่ต้องส้รางครับไม่มีไร เป้นรูปแบบของมัน เป้นการสรา้ง matrix
  104.  
  105.         if (fromleft >= fromright) { // ดูว่าเอียงไปทางไหนครับ ซ้ายหรือขวา
  106.             cv2DRotationMatrix(center, -angle, 1.0, rot); // อันนี้เอาข้อมูลไปเก็บไว้ใน matrix ไม่มีไรเกี่ยวข้องกับข้าว
  107.  
  108.         } else {
  109.             cv2DRotationMatrix(center, angle, 1.0, rot); // เหมือนกัน
  110.  
  111.         }
  112.  
  113.      cvWarpAffine(dest, dest, rot); // อันนี้สั่งหมุนครับ โดยเอา matrix ที่ทำไว้มาทาบเฉยๆ arg ตัวแรกคือรุปที่เอาทำ ตัวสองคือ output ตัวสามคือ matrix ที่เอามาทาบกับ arg ตัวแรก เพื่อให้ได้ตัวสอง output
  114.    
  115.  
  116.         return dest;
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement