Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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: 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.