views:

143

answers:

2

I'm attempting to create a true mosaic application. At the moment I have one mosaic image, ie the one the mosaic is based on and about 4000 images from my iPhoto library that act as the image library. I have already done my research and analysed the mosaic image. I've converted it into 64x64 slices each of 8 pixels. I've calculated the average colour for each slice and assertain the r, g, b and brightness (Luminance (perceived option 1) = (0.299*R + 0.587*G + 0.114*B)) value. I have done the same for each of the image library photos.

The mosaic slices table looks like so.

slice_id, slice_image_id, slice_slice_id, slice_image_column, slice_image_row, slice_colour_hex, slice_rgb_red, slice_rgb_blue, slice_rgb_green, slice_rgb_brightness

The image library table looks like so.

upload_id, upload_file, upload_colour_hex, upload_rgb_red, upload_rgb_green, upload_rgb_blue, upload_rgb_brightness

So basically I'm reading the image slices from the slices table into PHP and then pulling out the appropriate images from the library table based on the colour hexs. My trouble is that I've been on this too long and probably had too many energy drinks so am not concentrating properly, I can't figure out the way to pick out the nearest colour neighbor if the appropriate hex code doesn't exist.

Any ideas on the perfect query?

NB: I know pulling out the slices one by one is not ideal however the mosaic is only rebuilt periodically so a sudden burst in the mysql load doesn't really bother me, however if there us a way to pull the images out all at once that would also be a massive bonus.

Update Brightness Comparisons.

With Brightness

alt text

Without Brightness

alt text

A: 

I think that you may be better off using HSL instead of RGB as color space. Formulas to compute HSL from RGB are available on the internet (and in the linked Wikipedia article), they may give you what you need to compute the best match.

Lucero
Very promising! How I wish I knew enough on the topic to competently upvote!
Smandoli
Damn all that colour theory is way over my head.
buggedcom
Well, you may still try to use it: in HSL, instead of single color components, you talk about the acual color, saturation and intensity, which I feel is a better way of comparing differences than just by getting the closest RGB value.
Lucero
+1  A: 

One way to minimize the difference between the colours (in terms of their RGB components) is you would individually minimize the difference in each component. Thus you're looking for the entry with lowest

(targetRed - rowRed)^2 + (targetGreen - rowGreen)^2 + (targetBlue - rowBlue)^2
icio
You mean something like this?`SELECT upload_id, upload_zoom_1_file, upload_rgb_red, upload_rgb_green, upload_rgb_blue, ((70 - upload_rgb_red)^2 + (72 - upload_rgb_green)^2 + (76 - upload_rgb_blue)^2) AS ratio FROM uploads WHERE 1ORDER BY ratio ASCLIMIT 1`Where r = 70, g = 72 and b = 76. Trouble is there is a record with this exact rgb value and it's not returned. I've obviously misunderstood something here.
buggedcom
The ^ operator in MySQL isn't power as I was using in the psuedo code above -- sorry if I didn't make that clear. Instead you want to do something like the following, which I have tested: `SELECT *, (POW(70-r, 2)+POW(80-g, 2)+POW(90-b, 2)) as d FROM rgb ORDER BY d;`
icio
Ah excellent, thank you for pointing that out. I didn't even begin to think about it as the ^2 works in mysql. That works a treat. I presume adding another similar match to the brightness would also work?
buggedcom
The brightness is a function of the RGB components. If the R, G and B components are individually close to their targets then the brightness should be close to it's target. Yes, you can use this method to look for a close brightness, but I would like to point out that you needn't bother.
icio
Further, you could use `ABS(x)` instead of `POW(x, 2)` -- it's probably more efficient but might produce slightly different results. That's something you might like to test out.
icio
Interesting point thank you. I shall have a play.
buggedcom
Fyi, taking brightness into consideration does have an effect, see the updated post's images.
buggedcom