Assigning pixel values to a specific region - pixel

Say that I select a region in ImageJ. How can I for instance for the pixels in that region, give them the value of 1?
Thanks.

Run Image > Color > Color Picker... to choose the color value. The ImageJ status bar tells you the exact value while you move the mouse over the color.
Run Edit > Fill to fill the current selection with the current foreground color value.
By running the recorder (Plugins > Macros > Record...) while performing these commands, you get the answer how to do this e.g. in Java:
ImagePlus imp = IJ.getImage();
IJ.setForegroundColor(1, 1, 1); // this will result in value 1 in an 8-bit image
IJ.run(imp, "Fill", "slice");
Alternatively, you can use a lower level API call:
ImagePlus imp = IJ.getImage();
ImageProcessor ip = imp.getProcessor();
Roi roi = imp.getRoi();
ip.setColor(1);
ip.fill(roi);

Related

How to crop all object based on mask, Mask-RCNN

So I was working on instance segmentation but now I am just able to segment 1 of the object from the image.
So I run the below codes to get my output:
image2 = mpimg.imread(path_to_new_image)
# Run object detection
print(len([image2]))
results = model.detect([image2], verbose=1)
# Display results
ax = get_ax(1)
r2 = results[0]
visualize.display_instances(image2, r2['rois'], r2['masks'], r2['class_ids'],
dataset.class_names, r2['scores'], ax=ax, title="Predictions3")
And the output I get is as followed:
1
Then I run the following codes to crop the image based on the mask, but I only able to crop one of it...
2
So how can I crop both of them, instead of 1 mask only?
the reason you are getting only one region cropped is because you're taking your index to be r2['scores'].argmax(). This will only consider that building which has the highest score, blue region in your case.
Change the code from:
idx = r2['scores'].argmax()
To:
for idx in range(len(r2['scores']):
And apply some small modifications to make it run.

How to set the elevations of sea to a common negative value in GMT6.1?

I want to draw a relief of some region (sea and land involved), but I DO NOT want to show the variation of marine bathymetry, so I must set them to a common negative value to make sure that all the bathymetry map to the same color, like lightblue. I don't know how to do it?
That's easy using grdclip. When using command line GMT you can try:
gmt grdclip grid -Ggrid_clipped -Sb0/-1000 -V
which sets all values of your input grid grid< 0 to -1000 (adjust the values for your needs) and writes the clipped grid to outfile grid_clipped.
If you're using PyGMT you can try:
import pygmt
grid = pygmt.grdclip(grid, below = [0, -1000])
where you can directly hand the clipped grid to another PyGMT function for plotting like
fig.grdimage(grid = grid).

What is the syntax of ImageResize()

I have data that change in size and want to display them in the same window. The command
void ImageResize( BasicImage im, Number num_dim, Number... )
seems like a potential fit, but the syntax is not clear at all.
Let's say I have 512x5 data set and now it needs to be 367x5.
The , Number...) indicates that this command takes a different number of parameters, all of them interpreted as number parameters. Commands which do this, usually use one of their other parameters to specify how many such parameters follow.
A typical example for this is also the SliceN command.
In this particular case, the command not only allows you to change the size of the dimensions in the image, but also the number of dimensions. It is a very useful command to f.e. change a 2D image into a 3D stack or the like.
The command ImageResize( BasicImage im, Number num_dim, Number... ) does several things:
It replaces im in-place, so the meta-data, display and window remains the same
It adjusts the dimension calibration when the dimension size is changed. Here, the assumption is, that the field-of-view before and
after the resize is the same. (The command can be used to easily scale
images as shown in the example below.)
All values of the image im are set to zero. ( If you need to keep the values, you need to act on an image clone!)
Example 1: Resizing image with bilinar interpolation
image before := GetFrontImage()
number sx, sy
before.GetSize(sx,sy)
number factor = 1.3
image after := before.ImageClone()
after.ImageResize( 2, factor*sx, factor*sy ) // Adjusts the empty container with meta-data
after = warp(before, icol/factor, irow/factor ) // interpolate data
after.ShowImage()
Example 2: Extend 2D image into 3D stack
number sx = 100
number sy = 100
image img := RealImage("2D",4,sx,sy)
img = iradius* Random()
img.ShowImage()
OKDialog("Now into a stack...")
number sz = 10
img.ImageResize(3,sx,sy,sz) // All values are zero now!
img = iradius * Random()

Image blending with mask

I'm trying to combine the two images based on the information from the mask. I'm using the color information from the background image if the mask is 0 and color information from foreground image if the mask is 1. Because the mask and both
Images are of the same size, I would like to use logical indexing of matrices to achieve this.
My attempt:
mask = imread('mask.png');
foreground = imread('fg.jpg');
background = imread('bg.jpg');
[r,c,~]=size(mask);
A = zeros(size(mask));
for i=1:r
for j=1:c
if mask(i,j) == 0
A(i,j,:) = background(i,j,:);
end
if mask(i,j) > 0
A(i,j,:) = foreground(i,j,:);
end
end
end
imshow(A);
The result looks like a flickering blue image, but I don't want that. Please help.
You can do this a bit more concisely:
f = double(foreground).*double(mask);
b = double(background).*double(~mask);
blend = f+b;
imshow(blend, []);
Using logical indexing you could also do
foreground(logical(mask)) = 0;
background(logical(~mask)) = 0;
blend = foreground+background;
The ISNOT operator '~' inverts your matrix in the second line, so you cut out the area you would like for background.
NOTE: This works for black and white (one channel). For coloured images see rayryeng's solution.
There are two problems with your code. The first problem is that you are trying to assign colour pixels to the output image A, yet this image is only two-dimensional. You want an image with three channels, not two. In addition, the output image type you are specifying is wrong. By default, the output image A is of type double, yet you are copying values into it that aren't double... most likely unsigned 8-bit integer.
As such, cast the image to the same type as the input images. Assuming both input images are the same type, initialize your A so that:
A = zeros(size(foreground), class(foreground));
This correctly makes a colour image with the same type as any of the inputs, assuming that they're both the same type.
Now, your for loop is fine, but it's better if you do this in one shot with logical indexing. If you want to use logical indexing, create a new image that's initially blank like what you've done, but then make sure your mask has three channels to match the number of channels the other images have. After, you simply need to index into each image and set the right locations accordingly:
mask = imread('mask.png');
foreground = imread('fg.jpg');
background = imread('bg.jpg');
[r,c,d]=size(mask); %// Change
%// If your mask isn't three channels, make it so
%// Change
if d ~= 3
mask = cat(3, mask, mask, mask);
end
A = zeros(size(foreground), class(foreground)); %// Change
A(mask) = foreground(mask); %// Assign pixels to foreground
A(~mask) = background(~mask); %// Assign pixels to background
imshow(A);

Cubism.js / d3.js Scale and Extent

Can someone provide some insight on how scales and extents work together in cubism.js
.call(context.horizon()
.extent([-100, 100])
.scale(d3.scale.linear().domain([-10,10]).range([-100,100])
)
);
For example what does the code above do? If the values are generated using a random number generator (numbers between -10 and 10)
I know extent is used to set the maximum and minimum.
I know how to define a scale, example:
var scale = d3.scale.threshold().domain([100]).range([0,100])
console.log(scale(1)) // returns 0
console.log(scale(99.9)) // returns 0
console.log(scale(88.9)) // returns 0
console.log(scale(100)) // returns 100
I read about d3.scales here http://alignedleft.com/tutorials/d3/scales/
My main issue is that I want to define thresholds for my data, very simple
0-98 Red
98-100 Pink
100 Blue
Or maybe just
0-99.99 Red
100 Blue
But I'm not being able to use all what I've read to construct something that works.
I'm guessing that you just want to use a different color to represent anomalies in your data. If that is true, you don't need to create a domain and range.
You can just create a custom color palette like this:
var custom_colors = ['#ef3b2c', '#084594', '#2171b5', '#4292c6', '#6baed6', '#9ecae1', '#c6dbef', '#deebf7', '#f7fbff', '#f7fcf5', '#e5f5e0', '#c7e9c0', '#a1d99b', '#74c476', '#41ab5d', '#238b45', '#006d2c', '#00441b'];
This color palette was constructed using the palette on this page with an extra red color tacked on to the end.
Then just call the custom colors like this:
d3.select("#testdiv")
.selectAll(".horizon")
...
.call(context.horizon()
.colors(custom_colors)
));
Play around with the colors until you find a combination that you like. In this above example, only the outlier will be in red while the rest will follow the blue and green pattern.
Hope this helps!

Resources