How to resize an image in Julia? - julia

I am trying to use some pre-trained computer vision models which require images of a certain dimension. I need to re-size my custom dataset to be a certain dimension. How can I do this with Julia?

In Julia, you can use the imresize function as follows:
using Images
img_square = imresize(img_source, (224, 224))
where img_source is the original image which you want to resize and 224x224 is the new image size. You can find out more about the resizing operations in the Julia Images docs.

Related

How to crop an image in Julia?

I current have some images which contain a border that I need to remove so that I can use those images for a computer vision ML project. How can I use Julia to crop the images and remove the border?
In Julia, you can do the following:
using Images
img_source = testimage("lighthouse")
# where testimage is function from the `TestImages` package.
# You can then do the following:
new_img = img_source[100:200, 100:200]
where img_source[y1:y2, x1:x2] represents the coordinates of the subsection of the original image you want to use. You can read more about this in the Julia Images docs.

How to use JuliaImages to create a smaller image given a starting image?

I just got done exploring the docs for JuliaImages found here. What I want to do is as follows:
I have an image. It is a map of sorts. It takes up a lot of space so I want to index into the image and create a new smaller image that is just essencially a zoomed-in version of the original image. I know I could do this manually, but I want to create a re-usable script that I can use to apply this operation to N number of images. How can I do this using JuliaImages?
If by "zoomed in" you mean focusing on a small portion of the image and making it look bigger, you can do this with ordinary array-indexing tools. For example, img[251:500,147:328] would extract a portion of the image.
If what you're really looking for is a thumbnail, my favorite approach is to use restrict. That is limited to 2-fold reductions. You can also imfilter (best with the IIRGaussian filters of ImageFiltering.KernelFactors) and then call imresize. But there will be no beating the performance of restrict.

Multiple images in scilab simultaneously

How do we display more than one image simultaneously in scilab? I used to do it using figure,imshow() in matlab. Whats the scilab alternative?
Thank you.
Multiple imshow windows
Assuming you are using SIVP for image processing in SciLab, it is currently only possible to show one imshow window at a time.
Quote from the SIVP imshow documentation :
Bugs and Shortcomings
Only one imshow window can pop up.
Workaround
A workaround is to combine multiple images and show that image, for example:
// Two example images
im1 = ones(400,600);
im2 = rand(400,600);
// Put images side by side
im3 = [im1 im2];
imshow(im3);
// or put them top-to-bottom
im3 = [im1 ; im2 ];
imshow(im3);
When combining images be sure to have matching dimensions, so when placing images side-by-side the number of rows must match and when placing them top-to-bottom the number of columns must match.

How do I export a higher resolution image of a Mathematica Graph object?

How do I export a re-sized version of the output I get from a call to GraphPlot
(or TreePlot if they produce different output) to a jpg file?
Currently, I'm simply calling Export[file_name, G]
where G is the result from a call to something like GraphPlot.
I'm using Microsoft office picture manager to view the jpgs,
but re-scaling them there yields unsatisfactory results due to poor resolution
(the graph I'm trying to plot has strings as labels which can't be made out after rescaling this way). I would like to be able to choose the size/resolution of the rendered jpg.
As Simon already pointed out, don't use a raster-format for a vector-graphics. Instead, export you plot to e.g. a scalable vector graphics:
graph = GraphPlot[ExampleData[{"Matrix", "HB/can_292"}, "Matrix"]];
Export["graph.svg", graph]
The advantage is, that in such an image, you can still adjust and change lines, polygons and colors. And finally, you can export it to an image of arbitrary quality easily.
And remember, for Plots which contain lines, polygons, ... everything with sharp edges you should never use jpg. General speaking, this is a format for photographs only since its compression is made for reducing filesize in natural images. In those images you don't recognize the compression, in images with text, lines and polygons you certainly will notice the artefacts. Use png if possible. Take your browser and zoom into the above image.
You can set both image size and compression level of the exported file by doing something like
Export[file_name, G, ImageSize -> 1200, "CompressionLevel" -> 0]
The best way I find is to use ImageResolution property. It Increases the resolution of exported image but does not change the scale. Use it like this:
Export[ "image-file-name.png", image, ImageResolution -> 500 ]
Set the size of your graph before exporting it:
Graph[theGraph, ImageSize->2000]

Qt and exporting vector images

I am using QPainter to draw some graphs on the screen and save these graphs as a raster images.
Is it possible to use the same logic I used for rasterizing these graphs to have them exported to vector images that I can use in a PDF?
You can use a QSvgGenerator as a "backend" to your QPainter, and you'll get an SVG document out of it. (The documentation has an example for how to do that.)
I don't know if you can use that directly in PDFs, but there are converters out there if you need a different vector graphics format.

Resources