1

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
(source: buggedcom.co.uk)

Without Brightness

alt text
(source: buggedcom.co.uk)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
buggedcom
  • 1,537
  • 2
  • 18
  • 34

2 Answers2

1

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
  • 3,060
  • 20
  • 22
  • 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 1 ORDER BY ratio ASC LIMIT 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 May 07 '10 at 00:55
  • 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 May 07 '10 at 01:17
  • 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 May 07 '10 at 01:31
  • 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 May 07 '10 at 12:08
  • 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 May 07 '10 at 12:09
  • Fyi, taking brightness into consideration does have an effect, see the updated post's images. – buggedcom May 07 '10 at 14:20
0

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
  • 59,176
  • 9
  • 122
  • 152
  • Very promising! How I wish I knew enough on the topic to competently upvote! – Smandoli May 07 '10 at 01:03
  • Damn all that colour theory is way over my head. – buggedcom May 07 '10 at 01:07
  • 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 May 07 '10 at 01:11