Ask Your Question
-2

Equivalent OpenCV Java Code to this C++ Code

asked 2018-03-08 09:48:34 -0600

Udaib khan gravatar image

updated 2019-12-10 01:22:42 -0600

Akhil Patel gravatar image

can anybody tell me the correct java code for this c++ code snippet:

 output.at<cv::Vec4b>(x, y) = target.at<cv::Vec4b>(dx, dy);

I have tried this java code and they are displacing pixel but not showing image clearly :

output.put(x, y, target.get(dx, dy));
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-03-13 22:11:25 -0600

phillity gravatar image

updated 2018-03-14 11:15:38 -0600

EDIT: I should add that you should not do per pixel operations in general. There are many optimized OpenCV functions which can complete the majority of tasks you will encounter. This means that performing per pixel operations is bad practice!

Here is an example of accessing (get) and placing (put) pixel contents in Java

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;

public class ForumAnswer {
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat input = Imgcodecs.imread("lena.png");
        Mat copy = new Mat(input.size(), input.type());

        for(int i = 0; i < input.rows(); i++)
        {
            for(int j = 0; j < input.cols(); j++)
            {
                // each copy.get(i, j) will return a double array with 3 values - 
                // the blue intensity value, the green intensity value and the red intensity value (in that order!!)
                // of pixel (i,j)
                copy.put(i, j, input.get(i, j));
            }
        }

        Imgcodecs.imwrite("copy.png", copy);
    }
}

And here is the corresponding C++ code

#include <opencv2/opencv.hpp>
#include <iostream>

int main()
{
    cv::Mat input = cv::imread("lena.png");
    cv::Mat copy(input.size(), input.type());

    for (int i = 0; i < input.rows; i++)
    {
        for (int j = 0; j < input.cols; j++)
        {
                    // input BGR color image has three channels so we use Vec3b (if we had four channels we would use Vec4b)
                    // the b in Vec3b means uchar (we could use Vec3f if the Mat contained float values)
                    // read the documentation here to learn more https://docs.opencv.org/trunk/dc/d84/group__core__basic.html#ga595458e63aa1443ddd5c51fa71d70e2a
            copy.at<cv::Vec3b>(i, j) = input.at<cv::Vec3b>(i, j);
        }
    }

    cv::imwrite("copy.png", copy);

    return 0;
}

Hope this helps. If not, post more of your code so we can identify the problem.

edit flag offensive delete link more

Comments

@phillity i have having issue at overlay method where i overlay foreground over background using opacity Here is code snippet:

 double opacity = foreground.get(fY, fX)[2]/ 255.;
            // and now combine the background and foreground pixel, using the opacity, but only if opacity > 0.
            for (int c = 0; opacity > 0 && c < output.channels(); ++c) {
                double backgroundPx= background.get(fY, fX)[2] / 255.+c;
                double foregroundPx = foreground.get(fY, fX)[2] / 255.+ c;
                //line 06
                output.put(y, x, ((backgroundPx * (1. - opacity)) + (foregroundPx * opacity)));
            }

and the error is : Provided data element number (1) should be multiple of the Mat channels count (3) at line 06

Udaib khan gravatar imageUdaib khan ( 2018-03-14 05:33:21 -0600 )edit

First - I cannot really see what you are trying to accomplish here with you given code, so there may be other issues beyond this fix.

Second - more than likely, there is some way to accomplish what you are doing without performing a per pixel operation. You should step through your C++ code that you are trying to convert and understand clearly what is going on! This way you can you use OpenCV functions to complete the transition smoothly.

phillity gravatar imagephillity ( 2018-03-14 10:59:09 -0600 )edit

Either way, the error you are getting is because your output is a 3-channel Mat. That means each pixel location of output stores an array of THREE values. You are only trying to place ONE value (result of ((backgroundPx * (1. - opacity)) + (foregroundPx * opacity))) into a pixel which should contain an array of THREE values. This is incorrect and why you are getting the error.

//line 06
output.put(y, x, ((backgroundPx * (1. - opacity)) + (foregroundPx * opacity)));
phillity gravatar imagephillity ( 2018-03-14 11:12:27 -0600 )edit

@phillity here i have two images one is background i.e shirt image and other is foreground image that is logo image so i want to place logo image over shirt image such that logo conform to the curvature of shirt image using overlay and displacement map filter technique here in above its snippet from overlay part. let me attach here overall code snippet then you will understand it easily

Udaib khan gravatar imageUdaib khan ( 2018-03-14 13:03:39 -0600 )edit

I cannot do the whole conversion for you. You should read the OpenCV documentation and tutorials. Then, once you have an okay understanding of basic OpenCV operations and data structures, step through the C++ code to understand how it works. Once you understand exactly what it is doing, you can convert it to Java. You can use this forum when you run into specific problems/questions. It will be challenging to learn OpenCV at first, but, it will be very rewarding if you learn it yourself. Good luck

phillity gravatar imagephillity ( 2018-03-14 13:42:08 -0600 )edit

@phillity here you can see my code where i have first done displacement and then overlay part please have a look at this one file Displacement and Overlay Code

Udaib khan gravatar imageUdaib khan ( 2018-03-14 13:47:21 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-03-08 09:48:34 -0600

Seen: 1,331 times

Last updated: Mar 14 '18