Advertisement
Guest User

Untitled

a guest
Feb 17th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.79 KB | None | 0 0
  1. import javax.swing.JFrame
  2. import org.bytedeco.javacv.{CanvasFrame, OpenCVFrameConverter}
  3. import org.bytedeco.javacpp.opencv_core, opencv_core._
  4. import org.bytedeco.javacpp.opencv_imgcodecs._
  5. import org.bytedeco.javacpp.opencv_imgproc._
  6. import java.io.File
  7. import java.util.Random
  8.  
  9. object Main extends App {
  10.  
  11.   def reduceRect(r: Rect, n: Int = 2): List[Rect] = {
  12.     if (r.width > (r.height * 1.15)) {
  13.       val w = r.width / n
  14.       if (w > (r.height * 1.15))
  15.         reduceRect(r, n+1)
  16.       else
  17.         (0 until n).map { i =>
  18.           new Rect(r.x + (i * w) + 1, r.y, w - 2, r.height)
  19.         }.toList
  20.     } else r :: Nil
  21.   }
  22.   val img = imread(new File(
  23.     getClass.getClassLoader.getResource("10_1_pull.png").toURI).getAbsolutePath)
  24.   if (img.empty())
  25.     sys.error("Empty!!!")
  26.   val erodeKernel = getStructuringElement(MORPH_ELLIPSE, new Size(48,48))
  27.   val gray = new Mat
  28.   cvtColor(img, gray, CV_BGR2GRAY)
  29.   val copy = new Mat
  30.   gray.copyTo(copy)
  31.   threshold(gray, gray, 37, 255, 0)
  32.   dilate(gray, gray, erodeKernel)
  33.   erode(gray, gray, erodeKernel)
  34.  
  35.   val mask = new Mat(gray.rows, gray.cols, CV_8UC1)
  36.   val rectmask = new Mat(gray.rows, gray.cols, CV_8UC1)
  37.   val rectmask1 = new Mat(gray.rows, gray.cols, CV_8UC1)
  38.  
  39.   val contours = new MatVector
  40.   val hierarchy = new Mat
  41.   val bottom = 0.85
  42.   val top = 0.29
  43.  
  44.   val nohigher = img.rows * top
  45.   val nolower = img.rows * bottom
  46.  
  47.   val WHITE = new Scalar(255,255,255,0)
  48.   val BLACK = new Scalar(0,0,0,0)
  49.   findContours(gray, contours,
  50.     CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, new Point(0, 0))
  51.   val r = new Random
  52.   (0 until contours.size.toInt) foreach { c =>
  53.     val rect = boundingRect(contours.get(c))
  54.     val area = contourArea(contours.get(c))
  55.     if (rect.y + rect.height < nolower && rect.y > nohigher && area > 10000) {
  56.       reduceRect(rect) foreach { r1 =>
  57.         val color = new Scalar(r.nextInt(255), r.nextInt(255), r.nextInt(255), 0)
  58.         if (rectmask.isNull) {
  59.           rectmask.zero()
  60.           rectmask1.zero()
  61.         }
  62.         rectangle(img, r1, WHITE, 8, LINE_8, 0)
  63.         rectangle(rectmask, r1, WHITE, FILLED, LINE_8, 0)
  64.         bitwise_not(rectmask, rectmask1)
  65. //        drawContours(img, contours, c, color, FILLED, LINE_8, hierarchy, 0, new Point)
  66.         drawContours(rectmask1, contours, c, WHITE, FILLED, LINE_8, hierarchy, 0, new Point)
  67.         rectmask.copyTo(mask, rectmask1)
  68.         rectangle(mask, r1, WHITE, 8, LINE_8, 0)
  69.       }
  70.     }
  71.   }
  72.  
  73.   val masked = new Mat
  74.   img.copyTo(masked, mask)
  75.   val canvas = new CanvasFrame("My Image", 1)
  76.   canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  77.   val converter = new OpenCVFrameConverter.ToMat()
  78.   val out = converter.convert(masked)
  79.   canvas.setCanvasSize(out.imageWidth / 4, out.imageHeight / 4)
  80.   canvas.showImage(out)
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement