Okay so here's a good one for you guys, I have some theories but would like some help.

So I have a project I just came up with while I was waking up this morning. Part of this project involves comparing 2D arrays (or Lists, if I say array assume List could replace it).

Now the idea is I have a main 2D array of all int values, and then a collection of 2D arrays that I am comparing to this main 2D array. I want to find the array that is closes to the main array.

I know something like an average would probably not work well here. One idea I had was subracting the 2D array from the collection, from the main 2D array, creating a new 2D array. Each value would then be compared to a threshold value. If the value is under the threshold value it would recieve a 1, if it was over it would recieve a 0 (note that negatives would be converted to positives). This new 2D array of binary bits would be used to determine the array that has the most 1 bits (which would be the closes 2D array).

Another theory I thought of was using Hamming Code, as it's used for comparison, but I really have no clue how to implement this, as I can't remember if I have even doing Hamming Code by hand.

So all you geniuses out there, what to you have to offer here?

(Also note, the 2D might be turning into the point where 1 index could contain multiple values as well, so a 3D array, why I was thinking of the binary bit concept above)

Recommended Answers

All 7 Replies

Yopu might have to be a bit more specific on how you want to compare the arrays. If the arrays have the same values will they be in the same order? If the arrays are different sizes how do you want to handle that? if arrayA has 1,2,5,10 and arrayB has 1,3,4,10 the average will be the same but the values very different. If your arrays will represent objects it might be easier to cast them as the object to compare them.

commented: Good points! +14

Whoops, the arrays will all be the same size.

Also, there is no ordering, it simply needs to find the array that is the closes to the main array.

You also mentioned casting them as objects to compare them. Can this really produce the results I am looking for? Even if it's a 2D array? Pretty much the arrays contain a collection of data from (x,y points).

Remember that poster who asked about the mosaic question earlier this week? Yeah this is where the idea is stemming from. The array is pixel data from each pixel of an image.

I started writing up the algorithms for this this morning on how to possibly do it (for personal sake, I want to see if I could do it, hope no one takes this as me stealing his stuff, I just really liked the idea).

basically all you'll produce from comparing the arrays is whether the images have pixels in the same locations. You might have to tie the array to the image data itself and look to see if the pixels at the same location share the same color, and build a percentage score based on that.

Well sort of but not completely, we are not comparing in 1 to 1 ratio.

The concept is that I would choose an image I wanted to convert to a mosaic image, so I would load it in and the get the color code for each pixel (RGB, 3 values of 0-255). Then using that single pixel, I would look through a collection of images where I computers the average value of each image's pixels, to get a single pixel code value (again 3 values). I actually have just created a folder that contains over 6,000 images for this purpose (and am currently writing a program simply to extract their pixel values and store them in a database for later use).

So what I want to do is then search through this collection of images, and find which one has the closes average pixel value to the pixel value of the main image (well the current pixel I am on).

However, I realized that a conversion like this could cause issues with not getting proper transitions sometimes (like black to white where the main image goes dark to light from left to right, and the image I am going to insert does more of a light to dark from left to right ... if that makes sense).

So I came up with a possible way to improve this, but involves comparing 2D arrays to see which two are the closes

Like
[1, 2]
[2, 1]

is close to
[1, 1]
[1, 1]

then say
[1, 3]
[5, 1]

I hope that all makes sense

Could you not use some sort of standard deviation?
The picture with the smallest S.D. would be closest to your "mean" picture.

Well I could do something like this I guess, I mean the only thing I have to be careful of, is a photo from the collection might have a weird transition in colors that happen in a small fraction, which could throw off the pixel I am trying to replace it with.

For instance if the pixel I want to replace is black, and an image I find in the collection is mostly black but then has a spot of white somewhere, that's very different, the mean would support it, but it would look off wouldn't it?

Also to get the standard deviation, would I just take the average of all the pixels in the one image? (that image being an image from the collection) Or were you thinking of doing it a different way?

But to be honest I ran into another snag. I am trying to create a database of images that I can work with, but storing them as pixel data. I have written multiple compression algorithms, but can only get one image down to 13.1 mb from 45.4 mb (still a lot to store). All i can think of now is recursive compression (did learn the power of foreach and stringbuilder of for loops and +=)

I'll think more into that Standard Deviation, pull out some notes of mine from last year, see if I can find a way to implement it, but I think this is kind of one way I wanted to do it was take the average of an image

(sorry if I rambled, this is sometimes how I think, just spit balling ideas, and running tons of trials and errors in my head)

For anyone who may stumbole on this, I think I found my answer out.

I created an average pixel color of each image, adding up the red, green, and blue value of each pixel and then dividing by the total number of pixels.

Then I used the Euclidean Distance algorithm, or more simply the distance algorithm, to compare two images. The closer the images, the lower the value. So far I have only run small tests, but I feel this is what I was looking for

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.