Dimensions are not Ok in matlab - matlab

When I take image from img from commands =
imread('C:\Users\Administrator\Desktop\Detect\F04U2MPIXQG15WQS.LARGE.jpg');
figure,imshow(img)
It works fine but when I use
[filename, pathname] = ...
uigetfile({'*.jpg';'*.jpeg';'*.png';'*.*'},'Select Image File');
img=strcat(pathname,filename);
error shown is Error in skinDetect2Func (line 8)
yuv(:,:,y) = (img(:,:,r)+2.*img(:,:,g)+img(:,:,b))/4; Error in
bwFingers1 (line 39)
out=skinDetect2Func(img);
Anyone can help me out I am just stuck in here. :(
Here is the complete code:
function out=skinDetect2func(img)
imshow(img);
sz=size(img); r=1;g=2;b=3;y=1;u=2;v=3;
yuv=img;
region=yuv;
yuv(:,:,y) = (img(:,:,r)+2.*img(:,:,g)+img(:,:,b))/4;
yuv(:,:,u) = img(:,:,r)-img(:,:,g);
yuv(:,:,v)=img(:,:,b)-img(:,:,g);
region = (yuv(:,:,u)>20 & yuv(:,:,v)<74) .* 255;
toc;
out=region;
%filtering
out=im2bw(out); out=bwareaopen(out,100);
out=imdilate(out,strel('diamond',4));
%retain largest only
res=out;
cc=bwconncomp(res);
arr=(cellfun('length',cc.PixelIdxList));
newLabel=res;
if ~isempty(round(arr))
msz=0;
for i=1:length(arr)
if msz<arr(i:i)
msz=arr(i:i);
index=i;
end
end
labels=labelmatrix(cc);
newLabel=(labels==index);
out=newLabel;
end
out=imfill(out,'holes');
end***

ehm,
This is a string:
img=strcat(pathname,filename); % STRing conCATenation
You still need to imread...
The correct code would be
imgpath=strcat(pathname,filename);
img=imread(imgpath);

Related

why I get the error: Undefined function or variable 'shapes'

I'm working in matlab 2016.
I have a program code. It needs to work correctly.
Here is the program:
clear all;
addpath('..');
training_files = dir('cootes/*.bmp.mat');
for i=1:numel(training_files)
load(sprintf('cootes/%s', training_files(i).name));
app = imread(sprintf('cootes/%s', training_files(i).name(1:end-4)));
% Map RGB colors to [0,1]
appearances(:,:,:,i) = double(app) ./ 255;
shapes(:,:,i) = xy2ij(annotations, size(app,1));
end
load('cootes/triangulation.mat');
test_img = 2;
one_out = [1:test_img-1, test_img+1:size(shapes,3)];
AAM = build_model_2d(shapes(:,:,one_out), appearances(:,:,:,one_out), 'triangulation', triangulation);
fprintf('\n******************************************************* 2D FITTING *******************************************************\n\n');
disp 'Figure 1: leave-one-out fitting result (red mesh) using as intialization a random shape from the training set (blue mesh).'
disp 'Figure 2: reconstructed appearance.'
disp 'Usage: Hit a random key to use a different initialization shape. Use CTRL+C to quit.'
fprintf('\n');
while 1
init_shape = one_out(round(rand()*(numel(one_out) - 1) + 1));
try
[ fitted_shape fitted_app ] = fit_2d(AAM, shapes(:,:,init_shape) + repmat([-5 -5], [size(shapes, 1) 1 1]), appearances(:,:,:,test_img), 20);
figure(1)
imshow(appearances(:,:,:,test_img));
hold on;
triplot(AAM.shape_mesh, shapes(:,2,init_shape), shapes(:,1,init_shape), 'b');
triplot(AAM.shape_mesh, fitted_shape(:,2), fitted_shape(:,1), 'r');
hold off;
figure(2)
imshow(fitted_app);
pause;
catch ME
fprintf('Fitting diverged: %s\n', ME.message);
end
end
After starting the program appears the following error:
Undefined function or variable 'shapes'.
Error in annotate_test (line 29)
AAM = build_model_2d(shapes, appearances, 'triangulation', triangulation);
Сan anyone explain why the variable 'shapes' is not defined. Although clearly spelled out:
shapes(:,:,i) = xy2ij(annotations, size(app,1));
Please help, I'm newby and don't understand where can be the error.
training_files = dir('cootes/*.bmp.mat'); %error is here
try this.
training_files = dir(fullfile(mypath,'*.bmp.mat')); % should work if you have .bmp.mat files in that path

Embedding data from a text file into DWT subband using steganography

I am attempting to embed data from a text file(which contains only numeric data) into LL subband of an image using a steganography. I am getting an error "Error using bitset ASSUMEDTYPE must be an integer type name" in the line of code:
L(ii,jj)=bitset(L(ii,jj),1,stego(ii,jj));
I have attempted to run in debugger but I am having no luck. I think it must be something to do with the data type of L?? I have tried changing image to binary,double etc but I still get this error! Please can someone give me some advice on where I am going wrong?I have a insert my code below
% loading cover image
img=imread('lena.bmp');
image=im2double(img);
% get DWT of image
[LL,LH,HL,HH] = dwt2(image,'haar');
A = importdata('minutiaTest.txt');
I = dec2bin(A,8);
L=LL;
% determine size of LL subband
Mc=size(L,1); %Height
Nc=size(L,2); %Width
% determine size of message object
Mm=size(I,1); %Height
Nm=size(I,2); %Width
for ii = 1:Mc
for jj = 1:Nc
stego(ii,jj)=I(mod(ii,Mm)+1,mod(jj,Nm)+1);
end
end
for ii = 1:Mc
for jj = 1:Nc
L(ii,jj)=bitset(L(ii,jj),1,stego(ii,jj));
end
end
stego_image = idwt2(LL,LH,HL,HH,'haar');
imwrite(uint8(stego_image),'stego.bmp');
figure; imshow(stego_image,title('Stego image'));

Undefined function 'relabel' for input arguments of type 'int32'. Error in import_experiment_label (line 22) runs = relabel(run);

I got an error when I run a matlab code online. The error is:
Warning: Name is nonexistent or not a directory: ......\toolbox_misc
Undefined function 'relabel' for input arguments of type 'int32'.
Error in import_experiment_label (line 22)
runs = relabel(run);
I tried to download matlab toolbox misc online, but still cannot fix
the problem. Anyone can help me? Thank you so much!
The following is the original code:
% Load the text experiment-label file to cell
% Kinalizer: /share/Bot/Research/mvpa_data/Haxby_6_subjects
% mac: /Users/kittipat/Downloads/Research/Haxby_7_subjects
% subjID = 2;
inDir = ['/Users/kittipat/Downloads/Research/Haxby_7_subjects/subj',num2str(subjID),'/'];
inFile = 'labels.txt';
outDir = ['/Users/kittipat/Downloads/Research/Haxby_7_subjects/subj',num2str(subjID),'/matlab_format'];
fileID = fopen(fullfile(inDir,inFile));
% !!!!! Must remove "labels chunks" at the top of the txt file first
myCell = textscan(fileID, '%s %d');
fclose(fileID);
category_name = myCell{1};
run = myCell{2};
% Make sure the run numbers are well-ordered from 1 to R
addpath('../../../toolbox_misc/');
runs = relabel(run);
num_run = length(unique(runs));
num_time_stamp = length(runs);
% Make associate labels (needs input from user)
category_name_list = {'rest','face','house','cat','bottle','scissors','shoe','chair','scrambledpix'};
assoc_label_list = [0,1,2,3,4,5,6,7,8];
num_category = length(assoc_label_list); % including 'rest'
assoc_label = zeros(num_time_stamp,1);
regs = zeros(num_category,num_time_stamp);
for i = 1:num_time_stamp
assoc_label(i) = assoc_label_list(strcmp(category_name{i},category_name_list));
regs(assoc_label(i)+1,i) = 1; % 'rest' is column 1
end
regs_with_rest = regs;
regs = regs(2:end,:); % exclude "rest" in the 1-st column
num_category = num_category - 1; % exclude the "rest"
save(fullfile(outDir,'experiment_design'),...
'category_name',...% the category name for each time stamp
'assoc_label',...% the number label for each time stamp
'assoc_label_list',...% the mapping between category_name and assoc_label
'category_name_list',...% list of the category name
'num_category',...% number of categories excluding "rest"
'regs',...% the category matrix excluding "rest"
'num_run',...% number of runs in well-ordered integer
'runs'... % the run# for each time stamp
);
%% plot the figure
h1 = figure;
subplot(4,1,2); plot(assoc_label,'b.-');
xlim([1, num_time_stamp]);
set(gca,'YTick',0:max(assoc_label(:))); set(gca,'YTickLabel',category_name_list);
subplot(4,1,1); plot(runs,'r.-');
title('run number after relabeling --> runs'); xlim([1, num_time_stamp]);
subplot(4,1,3); imagesc(regs_with_rest);
title('original design matrix --> regs\_with\_rest');
set(gca,'YTick',1:(num_category+1)); set(gca,'YTickLabel',category_name_list);
subplot(4,1,4); imagesc(regs);
title('after "rest" is removed --> regs');
xlabel('time stamps');
set(gca,'YTick',1:num_category); set(gca,'YTickLabel',category_name_list(2:end));
print(h1,'-djpeg',fullfile(outDir,'experiment_design.jpg'));
I fix the relabel problem. Then I change part of the code as follows.
inDir = ['/D disk/MATLAB/R2014a/subjX/beta_extraction_for_Haxby_matlab_toolbox_v1_8/subj',num2str(subjID),'/'];
inFile = 'labels.txt';
outDir = ['/D disk/MATLAB/R2014a/subjX/beta_extraction_for_Haxby_matlab_toolbox_v1_8/subj',num2str(subjID),'/matlab_format'];
Another error comes:
import_experiment_label
Undefined function or variable 'subjID'.
Error in import_experiment_label (line 7)
inDir = ['/D disk/MATLAB/R2014a/subjX/beta_extraction_for_Haxby_matlab_toolbox_v1_8/subj',num2str(subjID),'/'];
How to fix this problem? I do not know what is the wrong here. Thank you guys!

Converting tiff images to avi in matlab

I'm trying to use VideoWriter to convert large amounts of tiff images into an avi video. I'm having trouble with the for loop to convert the images into a video. Here is the code I have so far:
function tif2avi
clc; close all;
[imagelist,p]=uigetfile('*.tif','MultiSelect','on',...
'Select LIST to plot'); pause(0.5); cd(p);
if ~iscell(imagelist); disp('imagelist not cell'); return; end;
outputVideo = VideoWriter('0424_rat01.avi');
outputVideo.FrameRate = 16;
outputVideo.Quality = 100;
open(outputVideo);
for i=1:numel(imagelist)
img=imread(imagelist(i));
writeVideo(outputVideo,img);
end
Can anyone help me out? I think the problem is in my for loop.
This is the error I get when I try to run the code:
Warning: No video frames were written to this file. The file may be invalid.
> In VideoWriter.VideoWriter>VideoWriter.close at 289
In VideoWriter.VideoWriter>VideoWriter.delete at 238
Error using imread>parse_inputs (line 476)
The filename or url argument must be a string.
Error in imread (line 335)
[filename, fmt_s, extraArgs] = parse_inputs(varargin{:});
Error in tif2avi (line 14)
img=imread(imagelist(i));
Your imagelist is a cell array and therefore should be accessed using curly braces (instead of regular parentheses).
Replace
img = imread( imagelist(i) );
with
img = imread( imagelist{i} );
and see what happens.
PS,
It is best not to use i as a variable name in Matlab.

Error using wavread Invalid Wave File. Reason: Cannot open file

i got error Error using wavread (line 67)
Invalid Wave File. Reason: Cannot open file.
Error in energy_per_frame (line 2) [inpu fs] = wavread(file);
this is the source code for function that call energy_per_frame.
[inpu fs] = wavread(file);
clc;clear;close all;
file = input('Enter name of wav file ', 's');
counter=0;
for test_number = 0:4
for test_number2 = 0:20
counter=counter+1;
reference = dtw_avg(test_number2);
test = energy_per_frame(file);
distance(counter) = dtw(test,reference);
end
This is source code for function energy_per_frame
[inpu fs] = wavread(file);
fn = fs/2;
bins = 512; %useable bins
frame_length = bins;
window = hamming(frame_length);
overlap = .5;
number_of_frames = (length(inpu)/(frame_length)/overlap) - 1;
What is wrong here?
Well, your function definition should look something like this
function [ output_args ] = energy_per_frame( input_args )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
end
It looks like your missing the function keyword.
However, if the first block is just supposed to be a script then you're making the call to wavread before you actually have the user input for the filename. Also, there is really no need to call clear etc. like that in a function.