X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fkaka%2Fcakelight%2FFrame.java;h=07a6ffd0744c8229eee43c25430af4a6cd9fec94;hb=03b67a7377d6d23d517d33e47f338bb7859596ed;hp=a16202490ab0a29d94a43a96d0186faef0199b5c;hpb=4a2d60564647052562fad28644904298ba83667b;p=kaka%2Fcakelight.git diff --git a/src/kaka/cakelight/Frame.java b/src/kaka/cakelight/Frame.java index a162024..07a6ffd 100644 --- a/src/kaka/cakelight/Frame.java +++ b/src/kaka/cakelight/Frame.java @@ -1,12 +1,11 @@ package kaka.cakelight; +import javafx.scene.paint.Color; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.Size; import org.opencv.imgproc.Imgproc; -import java.awt.*; - import static kaka.cakelight.Main.saveFile; import static kaka.cakelight.Main.timeIt; @@ -15,6 +14,7 @@ public class Frame { private Configuration config; private Mat colImage; private Mat rowImage; + private Mat converted; private Frame(byte[] data) { this.data = data; @@ -46,14 +46,21 @@ public class Frame { // timeIt("resizing", () -> Imgproc.resize(converted, resized, new Size(config.leds.cols, config.leds.rows), 0, 0, Imgproc.INTER_AREA)); // INTER_AREA is the best for shrinking, but also the slowest (~1.5 ms) // }); - Mat converted = new Mat(); - Imgproc.cvtColor(src, converted, Imgproc.COLOR_YUV2RGB_YUYV); - timeIt("model 1", () -> model1(converted, Imgproc.INTER_AREA)); - timeIt("model 2", () -> model2(converted, Imgproc.INTER_AREA)); + Mat cropped = src.submat( + config.video.crop.top, + config.video.height - config.video.crop.bottom, + config.video.crop.left, + config.video.width - config.video.crop.right + ); + converted = new Mat(); + Imgproc.cvtColor(cropped, converted, Imgproc.COLOR_YUV2RGB_YUYV); +// timeIt("model 1", () -> model1(converted, Imgproc.INTER_AREA)); +// timeIt("model 2", () -> model2(converted, Imgproc.INTER_AREA)); timeIt("model 3", () -> model3(converted, Imgproc.INTER_AREA)); // save(converted, "/home/kaka/test-converted.data"); // save(resized, "/home/kaka/test-resized.data"); - System.out.println("color: " + getPixel(ListPosition.BOTTOM, 0)); + src.release(); + cropped.release(); } private void model1(Mat src, int interpolation) { @@ -73,25 +80,32 @@ public class Frame { Imgproc.resize(src, rowImage, new Size(16, config.leds.rows), 0, 0, interpolation); } - public Color getPixel(ListPosition listPosition, int xy) { + public Color getLedColor(ListPosition listPosition, int xy) { switch (listPosition) { case LEFT: - return pixelToColor(rowImage, 0, xy); + return interpolatedRowColor(xy, 0, 1, 2); case RIGHT: - return pixelToColor(rowImage, config.leds.cols - 1, xy); + return interpolatedRowColor(xy, 15, 14, 13); case TOP: - return pixelToColor(colImage, xy, 0); + return interpolatedColColor(xy, 0, 1, 2); case BOTTOM: - return pixelToColor(colImage, xy, config.leds.cols - 1); + return interpolatedColColor(xy, 8, 7, 6); } return null; } + private Color interpolatedRowColor(int y, int x1, int x2, int x3) { + return pixelToColor(rowImage, x3, y).interpolate(pixelToColor(rowImage, x2, y), 0.65).interpolate(pixelToColor(rowImage, x1, y), 0.65); + } + + private Color interpolatedColColor(int x, int y1, int y2, int y3) { + return pixelToColor(colImage, x, y3).interpolate(pixelToColor(colImage, x, y2), 0.65).interpolate(pixelToColor(colImage, x, y1), 0.65); + } + private Color pixelToColor(Mat image, int x, int y) { byte[] rgb = new byte[3]; image.get(y, x, rgb); - System.out.println("r = " + rgb[0] + ", g = " + rgb[1] + ", b = " + rgb[2]); - return new Color(rgb[0], rgb[1], rgb[2]); + return Color.rgb(rgb[0] & 0xff, rgb[1] & 0xff, rgb[2] & 0xff); } private void save(Mat mat, String filepath) { @@ -101,6 +115,33 @@ public class Frame { } public byte[] getData() { - return data; + byte[] buff = new byte[(int) (converted.total() * converted.channels())]; + converted.get(0, 0, buff); + return buff; + } + + public Mat getColImage() { + return colImage; + } + + public Mat getRowImage() { + return rowImage; + } + + public Mat getConvertedImage() { + return converted; + } + + /** + * Creates a LED frame going clockwise from the bottom-left corner, sans the corners. + */ + public LedFrame getLedFrame() { + LedFrame frame = LedFrame.from(config); + int led = 0; + for (int i = config.leds.rows - 1; i >= 0; i--) frame.setLedColor(led++, getLedColor(ListPosition.LEFT, i)); + for (int i = 0; i < config.leds.cols; i++) frame.setLedColor(led++, getLedColor(ListPosition.TOP, i)); + for (int i = 0; i < config.leds.rows; i++) frame.setLedColor(led++, getLedColor(ListPosition.RIGHT, i)); + for (int i = config.leds.cols - 1; i >= 0; i--) frame.setLedColor(led++, getLedColor(ListPosition.BOTTOM, i)); + return frame; } }