I want to crop images in R. I use this code for my data, but I get error. It does not make sense because there is no img.crop: I found that the class of image is "cimg". Do you think I need to change it to array class? Do you know how?
## example where you know where to crop the image
img <- load.image('C:/image/17.jpg')
plot(img)
dim(img)
print(k)
img22 <- crop.image(img ,xleft=446,ybottom=7,xright=203,ytop=256)
split.screen(c(1,2))
screen(1)
image2(img,asp=1,main='Original')
screen(2)
image2(img22[[1]],asp=1,main='Cropped')
class(img)
Error in crop.image(img, xleft = 446, ybottom = 7, xright = 203, ytop = 256) :
object 'img.crop' not found
Related
I would like to subtract the mean of the image from itself given a batch of images.
Obviously, the tf.image.per_image_standardization is not what I want since I don't want to divide by the standard deviation.
And, frames_normalized = tf.map_fn(lambda frame: tf.reduce_mean(frame, axis=[2]), frames_contrast_adjust) is not what I want since that will reduce the dimensions of the original image. That is, if the dimension of a single image is [112, 112, 3] the result of the tf.reduce_mean image will has a shape of [112, 112]. Therefore, the size of frame_normalized will become: [?, ?, 112, 3] when frames_contrast_adjust is a batch of images of size: [?, 112, 112, 3].
Please note that I do want to use queues in the mean.
Any help is much appreciated!!
If you want the op which behaves almost like per_image_standadization but without variance, you can just look how tf.image.per_image_standadization is implemented and remove all the stuff related to variance (I commented it away):
image = ops.convert_to_tensor(image, name='image')
image = control_flow_ops.with_dependencies(_Check3DImage(image, require_static=False), image)
num_pixels = math_ops.reduce_prod(array_ops.shape(image))
image = math_ops.cast(image, dtype=dtypes.float32)
image_mean = math_ops.reduce_mean(image)
#variance = (math_ops.reduce_mean(math_ops.square(image)) -math_ops.square(image_mean))
#variance = gen_nn_ops.relu(variance)
#stddev = math_ops.sqrt(variance)
# Apply a minimum normalization that protects us against uniform images.
#min_stddev = math_ops.rsqrt(math_ops.cast(num_pixels, dtypes.float32))
#pixel_value_scale = math_ops.maximum(stddev, min_stddev)
pixel_value_offset = image_mean
image = math_ops.subtract(image, pixel_value_offset)
#image = math_ops.div(image, pixel_value_scale)
return image
The problem was solved by setting the keep_dims parameter in tf.reduce_mean to true. Now:
tf.map_fn(lambda frame: tf.reduce_mean(frame, axis=[2], keep_dims=True), frames_contrast_adjust)
And to subtract the mean of image from the image itself, I did the following:
frames_normalized = tf.map_fn(lambda frame: frame - tf.reduce_mean(frame, axis=[2], keep_dims=True), input_frames)
I wish to skeletonize this image
To do so i am using matlab's bwmorph function, Here is the snippet :
bw = bwmorph(img_bw,'skel',Inf);
However the output is not as expected. Here is the output.
Could someone suggest a better way to achieve proper results ?
EDIT: here is a stripped down relevant code
img = imread(name);
img = rgb2gray(img*4);
img_bw = img > 50;
img_bw = medfilt2(img_bw,[10 10]);
bw = bwmorph(img_bw,'skel',Inf);
What you see is aliasing, the imshow function can not display the full image because it is to large to fit the screen. To fit the screen some rows and columns are skipped, which cause the lines to be disconnected. To display an image at full resolution using a scrollpanel, use imscrollpanel
hFig = figure('Toolbar','none', 'Menubar','none');
hIm = imshow(bw);
hSP = imscrollpanel(hFig,hIm);
I'm working with some MRI data in Matlab 2014b, but the data is formed of intensity values not RGB. To get around this I use the code below to form a movie out of the MRI frames (I'm working on dynamic data here)
My problem is that the images need to have altered display values for the pixels, as the default only displays between -Inf and Inf, and I need between 0 and 0.25 to get a sensible image out of my data.
Are there any ways to pass that change from the script in to the movie, and then to write to file? I can only seem to do this per image in implay, and I'd like an automated way to edit each image and then store as a frame for a movie..?
%Code for producing movie.
graymap = gray(256);
for i = 1:32
a(:,:,i) = cmunique(Reformed_Data_Colourmap(:,:,i));
end
for i = 1:32
b = im2frame(a(:,:,i),graymap);
a(:,:,1) = ((b.cdata));
image(a(:,:,1))
colormap 'gray'
%The change needs to be here, to display pixel values from 0 to 0.25, to allow for a sensible image from the MR data.
frames(1,i) = getframe;
end
movie(frames)
The solution is provided:
for i = 1:32
b = im2frame(a(:,:,i),graymap);
a(:,:,1) = ((b.cdata));
clims = [0 250];
%image(a(:,:,1),clims)
colormap 'gray'
imagesc(a(:,:,1),clims);
%set('window', [0 400])
frames(1,i) = getframe;
end
clims solves the issue.
I'm trying to create a composite image from two original images, a background and an overlay which I manipulate using RMagick like follows:
background = ImageList.new("foo.png")
overlay_original = ImageList.new("bar.png")
overlay_resized = overlay_original.resize_to_fit(400,400)
overlay_cropped = overlay_resized.crop(NorthWestGravity, 400, 200)
new_image = ImageList.new
new_image = new_image.composite_layers(background)
new_image = new_image.composite_layers(overlay_cropped)
When I do this it gives me the following error: ArgumentError: no images in this image list
When I just try to check the length of the background and overlay_cropped image lists (background.length and overlay_cropped.length) it tells me the background image list has 1 image, but that overlay_cropped has no images:
NoMethodError: undefined method length' for bar.png PNG 640x1096=>400x200 400x400+0+0 DirectClass 8-bit:Magick::Image
Any ideas on what I'm doing wrong? I"m guessing the answer is pretty obvious.
Issue resolved by changing it to the following:
marketing_image = marketing_image.composite(background, NorthWestGravity, 0, 0, OverCompositeOp)
marketing_image = marketing_image.composite(overlay_cropped, NorthWestGravity, 327, 126, OverCompositeOp)
Let's say my image is img=zeros(100,100,3), my outputs are several ellipse which i get using a created function [ret]=draw_ellipse(x,y,a,b,angle,color,img), I can display one ellipse using imshow(ret).For the moment, I'm trying to show serval ellipse in the image. But i don't know how to code it. will ‘for loop’ work or I need to hold them?
If this is related to what you were doing in your previous question, then what you need to do is to pass the result of one iteration as input to the next.
So assuming that the function [ret]=draw_ellipse(x,y,a,b,angle,color,img) you mentioned takes as input an image img and returns the same image with an ellipse drawn on it, you could do this:
%# ellipses parameters
%#x = {..}; y = {..};
%#a = {..}; b = {..};
%#angle = {..}; color = {..};
img = zeros(200,100,'uint8'); %# image to start with
for i=1:10
img = draw_ellipse(x{i},y{i}, a{i},b{i}, angle{i}, color{i}, img);
end
imshow(img)
I'm a bit unsure of what you want. You want to show several ellipse in one image, like plotting several graphs with hold on?
There is no equivalent command for images, but a simple solution is to add the ellipses into one image and show that one:
several_ellipse = ellipse1 + ellipse2 + ellipse3;
imshow(several_ellipse)
Presumably you want to pass ret as the final input to the next call to draw_ellipse.