I'm trying to decipher how to troubleshoot why I have an error in my code.
Basically, I have a JPG image and I need to segment it through using thresholds on the three RGB channels.
This is my code:
img = cv2.imread('sample.jpg')
img = np.asarray(img)
img = img[:,:,0:3]
plt.imshow(img)
plt.show()
img_gray = rgb2gray(img)
img_gray = np.around(img_gray)
img_gray = img_gray.astype(int)
img_gray
histogram2, bins2 = np.histogram(img_gray, bins=range(255))
plt.bar(bins2[1:], histogram2)
plt.show()
img_select = np.where((img_gray[:,:,0]<150) & (img_gray[:,:,1]>70) & (img_gray[:,:,2]<90), 1, 0)
plt.imshow(img_select, cmap = 'gray')
plt.show()
The error is at the img_select part. I'm given this error:
"too many indices for array"
Can someone help me troubleshoot this?
Related
I am trying to plot a network of my ASVs using phyloseq using the plot_net function like so:
set.seed(711L) plot_net(Alaw_merged_30, distance = "bray", type = "taxa",maxdist = 0.7, color="Kingdom", point_label="Genus", laymeth = "auto", cex_val = 2)
My labels looked so small on my plot that I tried tried cex_val = 2 from the NeuralNetTools package but I keep getting this error:
Error in plot_net(Alaw_merged_30, distance = "bray", type = "taxa", maxdist = 0.7, :
unused argument (cex_val = 2)
Any help would be highly appreciated because my labels are so small on my plot currently!
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
Im trying to save an image sequence as a Tiff file to be later used in another program. I read each image in, applied color thresholding to get the mask, and then append that to my 3d array that I initialized earlier.
I read up on the Tiff class needed to write Tiff images and I think I set all the tags properly, but although the file gets created, I cannot open it in an image viewer. I get the error:
Invalid or Unsupported Tif file
There are 290 images in folder, so tiff file will have 290 planes. Below is how I am saving the tiff file:
clear;
path = 'D:\complete stack';
img_list = dir(fullfile(path, '*.tif'));
img = imread(fullfile(img_list(1).folder, img_list(1).name));
tiff_array = zeros(size(img, 1), size(img, 2), numel(img_list), 'uint8');
clear img;
for i = 1:numel(img_list)
img = imread(fullfile(img_list(i).folder, img_list(i).name));
[bw, ~] = createMask(img);
tiff_array(:,:,i) = bw;
end
t = Tiff('myfile.tif', 'w');
setTag(t,'Photometric',Tiff.Photometric.Mask);
setTag(t, 'Compression', Tiff.Compression.None);
setTag(t,'BitsPerSample',8);
setTag(t,'SamplesPerPixel',size(tiff_array, 3));
setTag(t,'ImageLength',size(tiff_array, 1));
setTag(t,'ImageWidth',size(tiff_array, 2));
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
write(t, tiff_array);
close(t);
thanks
edit:
A more reproducible, more minimal example:
clear;
img = ones(750, 750, 'uint8');
tiff_array = zeros(size(img, 1), size(img, 2), 290, 'uint8');
clear img;
for i = 1:290
bw = ones(750, 750, 'uint8');
% [bw, ~] = createMask(img);
tiff_array(:,:,i) = bw;
end
t = Tiff('myfile.tif', 'w');
setTag(t,'Photometric',Tiff.Photometric.Mask);
setTag(t, 'Compression', Tiff.Compression.None);
setTag(t,'BitsPerSample',8);
setTag(t,'SamplesPerPixel',size(tiff_array, 3));
setTag(t,'ImageLength',size(tiff_array, 1));
setTag(t,'ImageWidth',size(tiff_array, 2));
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
write(t, tiff_array);
close(t);
[cross-posted from stats and datascience]
I'm working on an animal classification problem, with the data extracted from a video feed. The recording was made in a pen, so the problem is quite challenging with a dark background and many shadows:
Initially I tried scikit-image, but then someone helped me with an advanced tool called crf-rnn (http://crfasrnn.torr.vision/) that does a great job segmenting and labelling objects in an image. I did the following:
import caffe
net = caffe.Segmenter(MODEL_FILE, PRETRAINED)
IMAGE_FILE = '0045_crop2.png'
input_image = caffe.io.load_image(IMAGE_FILE)
from PIL import Image as PILImage
image = PILImage.fromarray(np.uint8(input_image))
image = np.array(image)
mean_vec = [np.mean(image[:,:,vals]) for vals in range(image.shape[2])]
im = image[:, :, ::-1]
im = im - reshaped_mean_vec
cur_h, cur_w, cur_c = im.shape
pad_h = 750 - cur_h
pad_w = 750 - cur_w
print(pad_h, pad_w, "999")
im = np.pad(im, pad_width=((0, max(pad_h,0)), (0, max(pad_w,0)), (0, 0)), mode = 'constant', constant_values = 255)
segmentation = net.predict([im])
segmentation2 = segmentation[0:cur_h, 0:cur_w]
The resulting image segmentation is rather poor (although two cows are recognized correctly):
I use a trained crf-rnn (MODEL_FILE, PRETRAINED), which works well for other problems, but this one is harder. I would appreciate any suggestions on how to pre-process this sort of image to extract the shape of most cows.
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);