copying an image to another image's coordinates

5,363 views
Skip to first unread message

Tuncay

unread,
Dec 28, 2011, 9:45:48 PM12/28/11
to android-opencv
Hi all,
Guess there are 2 Mat images called 'a' and 'b'.
'b' has 3 times larger sizes than a.
And I want to copy or insert the image a in the middle of the image
b.
You can have a look into this sample( http://www.rudral.com/sample.jpg
).

In native c++ I was using this code,

cvSetImageROI( b, cvRect( a->width, a->height, a->width, a-
>height ) );
cvCopy( a, b);
cvResetImageROI( b);

But I dont know how to do it in android-opencv. Thanks for any help

Kirill Kornyakov

unread,
Dec 29, 2011, 1:31:18 AM12/29/11
to android...@googlegroups.com
Check image-manipulations sample. Here is the source code: https://code.ros.org/trac/opencv/browser/trunk/opencv/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsView.java. Canny output is inserted into a submat (ROI). And better to start migrating to C++ syntax, it is cleaner, safer, it is supported, it is simply better, and finally Java API is a clone of OpenCV C++ 2.x syntax.

-Kirill

Kirill Kornyakov

unread,
Dec 29, 2011, 1:35:51 AM12/29/11
to android...@googlegroups.com
Canny in this sample works inplace, but you should create bSubmat and call a.copyTo(bSubmat) method.

Tuncay

unread,
Dec 29, 2011, 4:44:23 AM12/29/11
to android-opencv
Hi Kirill,
Thanks, but this method didnt help me at all.
I want a final image of 'b' that has 'a' in the middle of it.
But this method and submat gives only a or some other useless results.
Or I couldnt just make it.
Here is the code;

b_sub=b.submat(a.rows(), a.rows()*2, a.cols(), a.cols()*2);
a.copyTo(b_sub);

I think i need a code to do exactly setting image roi, and resetting
it again. Any ideas?

陈卿

unread,
Dec 29, 2011, 5:37:05 AM12/29/11
to android-opencv
you can use the pixels in small image to assign to the large one.Just
like this...


for (int i = 0;i<smallImg.rows; i++) for(int j =0;
j<smallImg.cols; j++){

(largeImg.data + largeImg.step * (i + smallImg.rows))[(j +
smallImg.cols) * largeImg.channels() + 0]

= (smallImg.data + smallImg.step * i)[j *
smallImg.channels() + 0]; //if your mat is only 1 channel! if 4
channels,to assign every channel~
}
if the large one is just 3 times larger sizes than the small one.

It will be in the middle.
hope I can help you!

Tuncay

unread,
Dec 30, 2011, 3:38:21 AM12/30/11
to android-opencv


Hi 陈卿,
Thank you so much for your answer. I think this is the answer of my
question. I have edited the code as below.

for (int i = 0;i<a.rows(); i++)
{
for(int j =0;j<a.cols();j++)
{
(b.dataAddr() + b.step1() * (i + a.rows()))[j * b.channels()]=
(a.dataAddr() + a.step1() * i)[j *a.channels()];
}
}

But now, it gives an error like this,
-The type of the expression must be an array type but it resolved to
long.
There must be a small mistake in my code, and I couldn't find it
yet...

Tuncay

unread,
Dec 30, 2011, 4:06:12 AM12/30/11
to android-opencv
By the way I forgot to say that these images are Mat variables.

陈卿

unread,
Dec 30, 2011, 4:58:46 AM12/30/11
to android...@googlegroups.com
(b.dataAddr() + b.step1() * (i + a.rows()))[j * b.channels()]....you get a wrong pixel value!

should be  change to.... (b.dataAddr() + b.step1() * (i + a.rows()))[(j + a.cols()) * b.channels()] 

and try again!!!



2011/12/30 Tuncay <tuncayc...@gmail.com>

Chris Chen

unread,
Dec 30, 2011, 5:00:41 AM12/30/11
to android-opencv
good luck!!!

On 12月30日, 下午5时58分, 陈卿 <chenqing1...@gmail.com> wrote:
> (b.dataAddr() + b.step1() * (i + a.rows()))[j * b.channels()]....you get a
> wrong pixel value!
>
> should be change to.... (b.dataAddr() + b.step1() * (i + a.rows()))[(j +
> a.cols()) * b.channels()]
>
> and try again!!!
>
> 2011/12/30 Tuncay <tuncaycakma...@gmail.com>

Kirill Kornyakov

unread,
Jan 3, 2012, 3:17:11 AM1/3/12
to android...@googlegroups.com
Your code looks OK, and it works for me. This method must be the most effective, because JNI calls in a loop will kill your performance... So, better to understand why copyTo to submat doesn't work, it is canonical way. Here is my test and the output image:

    public void testCopyToSubmat() {
    int size = 30;
    Mat b = new Mat(size*3, size*3, CvType.CV_8UC1, new Scalar(0));
    Mat a = new Mat(size, size, CvType.CV_8UC1, new Scalar(255));
   
    Mat bSubmat = b.submat(a.rows(), a.rows()*2, a.cols(), a.cols()*2);    
    a.copyTo(bSubmat);
   
    Highgui.imwrite("mnt/sdcard/SubmatCopyToTest.png", b);
    }
SubmatCopyToTest.png

Tuncay

unread,
Jan 4, 2012, 9:10:10 AM1/4/12
to android-opencv
Hi Kirill and 陈卿 !
Thanks for your answers, both way is OK for me but Kirill's latest
test was so useful for me. Thanks!

c10

unread,
Apr 17, 2012, 4:21:30 PM4/17/12
to android...@googlegroups.com
Hey chris chen, i was trying to use your code and getting the same error.
-The type of the expression must be an array type but it resolved to 
long.  
can u please help me, as i really need some code to get and copy pixel values. I cannot get it right by using the get/put functions of Mat class.
Plz reply soon.

Andrey Pavlenko

unread,
Apr 18, 2012, 10:53:03 AM4/18/12
to android...@googlegroups.com
what OpenCV version do you use?
what are your problems using Mat::get() and put() ?

c10

unread,
Apr 19, 2012, 5:11:02 PM4/19/12
to android...@googlegroups.com
Hey thanx for replying,
But I figured out where I was wrong, I had to use it like this:
for(int i=0;i<a.rows();i++) 
for(int j=0;j<a.cols();j++)  
{
a.put(j,i,double...data[]);
}

can u give a reason why to interchange rows and columns?
Reply all
Reply to author
Forward
0 new messages