• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Image Processing in java

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I'm writing a code in java. In which, firstly, I need to select a particular portion of an image (Gray Image) based on some criteria and then I've to find the sum of all the pixel values of the selected portion. I am able to extract the required portion. But I'm getting stuck at finding sum. My code is as follows:


Error:: ArrayIndexOutOfBoundsException at line:: sum = sum + d[l][/color]

Please help me.
 
Bartender
Posts: 5071
189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Pallavi,

welcome!

what kind of image is this Gray_image? The method 'get(x,y)'
is unfamiliar to me.

Could it be that get(j, i) should be get(i, j)?
 
Marshal
Posts: 8428
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again Pallavi,

Before you start, please eliminate confusing parts:

1. Don't use variable name as "l". As you see it is nearly impossible to guess correctly what it is. More confusion appears here -> l=l+1;
2. Outter loop usually jas "i" variable for loop control, and inner has "j". For many people it can cause confusion too. It is conventional use this way: outter loop "i", inner loop: "j".
3. For the code readability you could assign rect.y+rect.height to something meaningful, the same also you could do with rect.x+rect.width.
4. double[] d. What "d" suppose to hold? If you know the answer, name it exactly as the answer instead of the "d".
5. sum also does not mean enough if all this code is not wrapped to a method which suppose to do certain task.

For the debugging ArrayIndexOutOfBoundsException you can add print statements by printing "l" value and you'll see what is wrong.
 
Piet Souris
Bartender
Posts: 5071
189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:(...)
3. For the code readability you could assign rect.y+rect.height to something meaningful, the same also you could do with rect.x+rect.width.


The code makes sense to me. What is it that makes it unclear to you?
 
Liutauras Vilda
Marshal
Posts: 8428
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:

Liutauras Vilda wrote:(...)
3. For the code readability you could assign rect.y+rect.height to something meaningful, the same also you could do with rect.x+rect.width.


The code makes sense to me. What is it that makes it unclear to you?


Maybe it is clear because you read:

Pallavi Vaish wrote:I need to select a particular portion of an image


I thought about selectedXAxisPortion or selectedYAxisPortion. Depends on taste.
I'm always trying to avoid expressions where possible and give some meaning to it.
 
Piet Souris
Bartender
Posts: 5071
189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see.

I was just wondering, since I myself would have written it like OP did.
albeit I would have used 'col' and 'row' instead of i and j,
to check if I did not mistakingly switch these around.

The width of a rectangle (in case of images) is the number of
colums, the height is the number of rows. Therefore the width
is the first parameter in the, say, 'getRGB(x, y)' method.
I noticed OP using it the other way round. Now, I don't know this
'get' method, so in case of OP it may be correct, but I was just asking.

I have made this mistake enough now to never make it ever again!
(really? No).
 
Pallavi Vaish
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Pallavi,

welcome!

what kind of image is this Gray_image? The method 'get(x,y)'
is unfamiliar to me.

Could it be that get(j, i) should be get(i, j)?



Hello Piet,

Thank you.

Gray_image is that portion that I extracted from the original image. Here I have used .get(x,y) to access the pixels of the Gray_image. And it's get(j,i) not get(i,j) because the outer loop is for j. It's just the variable name so I don't think order here really matters. I wanna ask that am I doing correct for accessing pixel? Please tell the way if I'm wrong. Thank you.
 
Saloon Keeper
Posts: 9565
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pallavi Vaish wrote:

Piet Souris wrote:hi Pallavi,

welcome!

what kind of image is this Gray_image? The method 'get(x,y)'
is unfamiliar to me.

Could it be that get(j, i) should be get(i, j)?



Hello Piet,

Thank you.

Gray_image is that portion that I extracted from the original image. Here I have used .get(x,y) to access the pixels of the Gray_image. And it's get(j,i) not get(i,j) because the outer loop is for j. It's just the variable name so I don't think order here really matters. I wanna ask that am I doing correct for accessing pixel? Please tell the way if I'm wrong. Thank you.


Well, without seeing your source code for Gray_image it's hard to tell. It's also hard to tell if it's (i,j) or (j,i). If you renamed the variables to 'x' and 'y' it would be clearer if you are right or wrong, that is, if you also supply the source code for get(int,int).
 
Pallavi Vaish
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess my question is not clear to anyone. To be very precise and clear, I want to find the sum of all the pixel values of a gray image for which I need to first access all pixel values. The gray image is of "Mat" data type. How can I do that? Please give an example if possible. Thank you!
 
Marshal
Posts: 76464
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain what you mean by sum. That would appear to vary more with the size of the image than its colour. Are you trying to work out averages? What does the get method return? Does it return an int in the range 0…0x00ff_ffff?
 
Piet Souris
Bartender
Posts: 5071
189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Pallavi,

I just googled for 'Mat' and 'get()'. It has to do with some OpenCV
library. Next time supply us with a little bit more information
when raising a question.

You use 'l' as the index for your 'double[] d' array. Now, for each new pixel,
you increase this 'l' by 1. No wonder that after two or three pixels you
get an AIOOB error.

Shouldn't you use a loop like:

 
Pallavi Vaish
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Solved!!
Thank you all for your replies!!
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic