Extract multiple images from white background

Hi all,

I’m very new to this, but very keen to understand how this might be achieved.

Basically I have several hundred image files which I’d like to extract individual photos from.

Each file is a photo of several photos, on a white background.

The sections (photos) won’t always be in the exact same position- but the background will be some form of white.

I tried explaining this with an image

Any ideas on how skikit-image might be able to achieve this?

that is easy

  1. make the mask: msk = imgs != (255, 255, 255)
  2. you can use scipy.ndimage.label, find_object to get some slices
    you can also use skimage.regionprop, but if you just want got the rect object, I think scipy.ndimage is the best choose.

@yxdragon Thanks!

Will that still work even if my background isn’t exactly white? As mentioned, it’s likely to be a lot more rough.

Also, I’m totally new to scikit-image so would you mind showing a bit more of the syntax or pointing me in the right direction?

that would be difficult, maybe you need some smooth filter, or transform to hsv or lab color space. And use a threshold such as:
msk = (255-img).max(axis=2)<30

you can provide some more demo image (background isn’t exactly white? As mentioned, it’s likely to be a lot more rough.)

skimage is a image processing lib, it is based on numpy.
some links may help:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.label.html
https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.find_objects.html

you even did not need scikit-image.
https://scikit-image.org/docs/stable/api/skimage.measure.html#regionprops
this function is powerful, but I think it is a little heavy for this question.

If the background is all a single colour, you can use flood_fill to find it.

We have a popular tutorial for scikit-image on YouTube, here:

The materials for the tutorial are here:

I recommend you have a look at it, as it’ll make all your downstream processing easier if you understand the basics.

NumPy also recently added a NumPy basics tutorial that is quite good if you don’t use NumPy often (the scikit-image tutorials assume you are familiar with NumPy).

1 Like

Thanks for the replies. Sorry for my delay.

  • Background is not at all a “single colour”
  • Size of each section is predictable (within a range)

In my basic understanding, a script could be written for scikit-image to “predict” the corner of the image, then stretch out the direction it guesses is the rest of the image exists? Maybe that is the wrong approach. I’m open to suggestions

I feel this might be pretty advanced. But I’m hoping I can leverage some smart tools/scripts that exist.

Here is a sample image of what I’m trying to extract from.

Hi
@yarnball
I use ImageJ
Paste the below macro code to an empty macro window (Plugin->New->Macro).
It will work with the image you posted. Not sure it will work with other pictures

setBackgroundColor(0,0,0);
setOption("BlackBackground",true);
img=getImageID();
run("Duplicate...", "title=1");
close("\\Others");
run("Duplicate...", "title=temp");
run("Invert");
run("Unsharp Mask...", "radius=10 mask=0.60");
run("8-bit");
setAutoThreshold("Moments dark");
//run("Threshold...");
//setThreshold(125, 255);
run("Convert to Mask");
run("Fill Holes");
run("Set Measurements...", "area bounding add redirect=None decimal=0");
run("Analyze Particles...", "size=200000-Infinity display include add");
count=roiManager("count");
for(i=0;i<count;i++)
{
roiManager("select",i);
selectWindow("1");
run("Restore Selection");
run("Duplicate...", " ");
run("Select None");
}
if (isOpen("ROI Manager")) { 
       selectWindow("ROI Manager"); 
       run("Close"); 
   }
if (isOpen("Results")) { 
       selectWindow("Results"); 
       run("Close"); 
   } 
close("temp");
close("1");
run("Tile");
exit("It's over.");

Good luck

Thanks for that suggestion. I’ve not heard of ImageJ. I created the. new macro (saved as “macro1.txt”), then went and clicked Macro > run > and seletected my image.

However - it returns the error:

Error:		Undefined variable in line 1:
		<JFIF> H H LExif MM * i 8 Photoshop 3 8 BIM 8 BIM % B ~ "ÿÄa	" 

Any ideas why? Any other suggestions?

Hi
@yarnball

You will find Image J here:

[https://imagej.nih.gov/ij/download.html]

Thanks, but I was not having trouble finding/downloading it.

My problem was when I run the macro, it gives the error from the above post. Any ideas on that?

Hi @yarnball
Paste the macro code to an empty macro window (Plugin->New->Macro) and run it with an image open in ImageJ.
The error message mentions photoshop! Are you doing a treatment with photoshop?

No I am doing nothing with Photoshop.

Is it something related to the macro/script you sent? I tried another image, and that one gave an error immediately saying:

Error:		Undefined variable in line 1:
		<JFIF> Exif MM * > F ( i N x 
``

i have made my fist attempt using scikit-image. I’ve followed the “segmentation” example.

Basically what I want to do, is extract on;y content inside the frame of the light green/blue. It looks to pretty much have segmented my sample image. How might I achieve this?

Here is how my result looks:

Original image for reference

And this is the code I would like to improve:

from skimage.future import graph
from skimage import data, segmentation, color, filters, io
from matplotlib import pyplot as plt


img = io.imread('./multi.jpg')
gimg = color.rgb2gray(img)

labels = segmentation.slic(img, compactness=10, n_segments=1500)
edges = filters.sobel(gimg)
edges_rgb = color.gray2rgb(edges)

g = graph.rag_boundary(labels, edges)
lc = graph.show_rag(labels, g, edges_rgb, img_cmap=None, edge_cmap='viridis',
                    edge_width=1.2)

plt.colorbar(lc, fraction=0.03)
io.show()
1 Like

Hi, Here is a Framework like ImageJ, but it is with scikit-image core. https://github.com/Image-Py/imagepy

  1. Open The Image

  2. Image > Type > 8-bit, Process > Features > Sato

  3. Image > Adjust > Threshold

  4. Process > Binary > Binary Convex Hull

  5. Analysis > Region Analysis > Geometry Analysis

So here is the macros, every step you can find the source code, it is all scikit-image’s function.

8-bit>None
Sato>{'start': 1, 'end': 5, 'step': 1, 'bridges': True}
Threshold>{'thr1': 0, 'thr2': 20}
Binary ConvexHull>None
Geometry Filter>{'con': '4-connect', 'inv': False, 'area': 10000.0, 'l': 0.0, 'holes': 0, 'solid': 0.0, 'e': -2.0, 'front': 255, 'back': 0}

Then you can use find_contours to get the vector contours. and mask the original image. May be you need check the 4 conner from the contours (it is easy), then do a perspective warp.

how to link video in markdown or image.sc?