簡體   English   中英

使用 OpenCV4Android 覆蓋圖像

[英]Overlaying image with OpenCV4Android

我一直在嘗試將圖像覆蓋在相機檢測到的矩形上。 我正在使用一個函數從我的 SD 卡 (.jpg) 返回我的圖像路徑,但它看起來沒有正確加載它,或者至少我無法在我想要的地方顯示它。

首先,我檢查圖像是否已加載(如果沒有,則加載它)。 然后,我繼續識別矩形。 一旦我認出了一個,我想用加載的圖像在框架上覆蓋那個矩形,然后返回到 ImageView 修改后的框架。

我將與您分享我在 onCameraFrame 函數中的代碼(“image”是一個 Mat 對象,我在其中存儲我從 sdcard 加載的圖像,而“img_path”是存儲圖像路徑的字符串):

Mat dst = inputFrame.rgba();

//Here I check if the image is loaded
        if (image.empty()){
            Log.v("Image","Empty!");
            image = imread(img_path, CV_LOAD_IMAGE_UNCHANGED);
        } else {

            //If the image is loaded, then I proceed to process the frame.
            Mat gray = inputFrame.gray();


            pyrDown(gray, dsIMG, new Size(gray.cols() / 2, gray.rows() / 2));
            Imgproc.pyrUp(dsIMG, usIMG, gray.size());

            Imgproc.Canny(usIMG, bwIMG, 0, threshold);

            Imgproc.dilate(bwIMG, bwIMG, new Mat(), new Point(-1, 1), 1);

            List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

            cIMG = bwIMG.clone();

            Imgproc.findContours(cIMG, contours, hovIMG, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);


            for (MatOfPoint cnt : contours) {

                MatOfPoint2f curve = new MatOfPoint2f(cnt.toArray());

                Imgproc.approxPolyDP(curve, approxCurve, 0.02 * Imgproc.arcLength(curve, true), true);

                int numberVertices = (int) approxCurve.total();

                double contourArea = Imgproc.contourArea(cnt);

                if (Math.abs(contourArea) < 100) {
                    continue;
                }

                //Rectangle detected
                if (numberVertices >= 4 && numberVertices <= 6) {

                    List<Double> cos = new ArrayList<>();

                    for (int j = 2; j < numberVertices + 1; j++) {
                        cos.add(angle(approxCurve.toArray()[j % numberVertices], approxCurve.toArray()[j - 2], approxCurve.toArray()[j - 1]));
                    }

                    Collections.sort(cos);

                    double mincos = cos.get(0);
                    double maxcos = cos.get(cos.size() - 1);

                    if (numberVertices == 4 && mincos >= -0.3 && maxcos <= 0.5) {
                        Rect r = Imgproc.boundingRect(cnt);
                        image.copyTo(dst.submat(r));

                        Log.v("Test 1",Integer.toString(image.width()));
                        Log.v("Test 2",Integer.toString(image.height()));

                    }
                }
            }
        }
        return dst;

我想查看圖像的矩形,但我一直在查看相同的未修改內容。 我只收到一次日志“空!”,所以它應該正確讀取圖像並將其存儲在我的墊子上。 另外,在我寫入圖像行和列數的其他 2 個日志中,我得到的數字大於 0。所以......

這是檢查圖像是否正確加載的正確方法嗎? 您現在知道為什么該代碼不起作用了嗎? 我還閱讀了 addWeighted 函數,但我嘗試調整較小圖像的大小,但失敗了。

提前致謝!!

編輯這是我識別矩形的代碼部分,我必須在框架上的矩形上渲染新圖像:

 //Rectangle detected
                if (numberVertices >= 4 && numberVertices <= 6) {

                    List<Double> cos = new ArrayList<>();

                    for (int j = 2; j < numberVertices + 1; j++) {
                        cos.add(angle(approxCurve.toArray()[j % numberVertices], approxCurve.toArray()[j - 2], approxCurve.toArray()[j - 1]));
                    }

                    Collections.sort(cos);

                    double mincos = cos.get(0);
                    double maxcos = cos.get(cos.size() - 1);

                    if (numberVertices == 4 && mincos >= -0.3 && maxcos <= 0.5) {
                        Rect r = Imgproc.boundingRect(cnt);
                        image.copyTo(dst.submat(r));

                        Log.v("Test 1",Integer.toString(image.width()));
                        Log.v("Test 2",Integer.toString(image.height()));

                    }
                }

例如,您可以從 InkHunter 應用程序的網頁查看以下圖片: https ://i1.wp.com/goosed.ie/wp-content/uploads/2016/10/inkhunter-cover-for-tattoo-ideas- 1.jpg?fit=800%2C450&ssl=1

我找到了解決方案。

圖像大小和圖像通道數必須相同,並使用掩碼更快地處理圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM