I have image data (for 40 people) and I am trying to detect face in each image, crop it and save it in another file. I am using MATLAB for this but it doesn't work.
ERROR : Unable to open file "C:\Users\mstfy\Desktop\Matlab\alex\newdata\cropped\" for writing. You might not have write permission.
I think there is something wrong in my for loop.
location = 'C:\Users\mstfy\Desktop\Matlab\alex\newdata\*.jpg';
croppedimg = 'C:\Users\mstfy\Desktop\Matlab\alex\newdata\cropped\';
imds = imageDatastore ( 'C:\Users\mstfy\Desktop\Matlab\alex\newdata' , ...
'IncludeSubfolders' , true, ...
'LabelSource' , 'foldernames' );
idx = randperm (numel (imds.Files), 16);
j = 1;
figure
for t = 1: 16
img = readimage (imds, idx (t));
FaceDetect = vision.CascadeObjectDetector;
FaceDetect.MergeThreshold = 7;
BB = step (FaceDetect, img);
for i = 1: size (BB, 1)
rectangle ( 'Position' , BB (i, :), 'LineWidth' , 3, 'LineStyle' , '-' , 'EdgeColor' , 'r' );
end
for i = 1: size (BB, 1)
J = imcrop (img, BB (i, :));
figure (3);
subplot (6, 6, i);
imshow (J);
j = j + 1;
imwrite (J,croppedimg,'jpg' )
end
end
Related
I need a guidance on a problem with linprog. I have an optimization problem that weights and the minimum value of the objective function are needed, but I can't allocate the parameters properly. The form of problem and subjects are like this. The picture of the problem is attached.
Any help regarding understanding the problem and getting a solution would be much appreciated. The picture below contains the formula for the problem.
IMAGE
P.S.: The object here is to code BWM(Best Worst Method) By Dr. Jafar Rezaei in matlab.
Ws and Ksi are the optimal weights and objective function we want to obtain and As are the vector that its values are present.
Here is the code in matlab.
clc;clear;close all;
delete Data.mat
DATA = xlsread("Data.xlsx");
NumOfExperts = size(DATA , 1) / 2;
NumOfCriteria = size(DATA , 2);
BestData = DATA( 1:NumOfExperts , : );
WorstData = DATA( NumOfExperts + 1: end , :);
WorstData = WorstData';
ConsistencyIndex = [0 , .44 , 1 , 1.63 , 2.3 ,3 , 3.73 , 4.47 , 5.23];
DATA = struct;
for i = 1:NumOfExperts
IndexOfBest = find(BestData(i , :) == 0);
IndexOfWorst = find(WorstData(: , i) == 0);
BestData(i , IndexOfBest) = 1;
WorstData(IndexOfWorst , i) = 1;
DATA(i).Best = BestData(i , :);
DATA(i).Worst = WorstData(: , i);
DATA(i).BestIndex = IndexOfBest;
DATA(i).WorstIndex = IndexOfWorst;
end
clear BestData .. WorstData .. IndexOfBest .. IndexOfWorst;
MeanWeight = zeros(1 , NumOfCriteria);
for i = 1:NumOfExperts
Data = DATA(i);
save Data
EqualCoefftMat = ones(1 , NumOfCriteria);
InitialPoints = rand(1 , NumOfCriteria);
LowerBound = zeros(1 , NumOfCriteria);
UpperBound = ones(1 , NumOfCriteria);
options = optimoptions(#fmincon , 'Algorithm' , 'interior-point' , 'MaxFunctionEvaluations' , 50000 , 'MaxIterations' , 5000 );
[DATA(i).Weights , DATA(i).Ksi] = fmincon(#Epsilon , InitialPoints , [] , [] , EqualCoefftMat , 1 , LowerBound , UpperBound , [], options);
DATA(i).ConsistencyRatio = DATA(i).Ksi/ConsistencyIndex(max(max(DATA(i).Best) , max(DATA(i).Worst)));
MeanWeight = MeanWeight + DATA(i).Weights;
end
MeanWeight = MeanWeight/NumOfExperts;
bar(MeanWeight);
xlabel('Criterias');
ylabel('Weights');
title(['Mean Of Weights is: ', num2str(mean(MeanWeight))]);
[Result.Weights , Result.IndexOfWeight] = sort(MeanWeight , 'descend');
function Out = Epsilon(x)
load Data
for i = 1:NumOfCriteria
f(i) = abs(x(Data.BestIndex)/x(i) - Data.Best(i));
g(i) = abs(x(i)/x(Data.WorstIndex) - Data.Worst(i));
end
Out = (sum(f) + sum(g) - g(Data.BestIndex))*2/NumOfCriteria;
end
I want to segment hand from a depth image using depth thresholding. I used this kinect and leap dataset from this link-
http://lttm.dei.unipd.it/downloads/gesture/
I tried these 2 codes, but the output I got is total black image in both the cases. The original .png image is
I selected depth value from 1_depth.bin file in the dataset.
Code 1
I = fopen('D:\dsktop\kinect_leap_dataset\acquisitions\P1\G1\1_depth.bin', 'r');
A = fread(I, 480*640, 'uint8=>uint8');
A = reshape(A, 480, 640);
min_row = min(A);
min_col = min(min_row);
for i = 1:480
for j = 1:640
if ((A(i,j) > (min_col + 10)) || (A(i,j) == (min_col + 10)))
A(i,j) = 1;
else
A(i,j) = 0;
end
end
end
imshow(A)
Code 2
image = imread('D:\dsktop\kinect_leap_dataset\acquisitions\P1\G1\1_depth.png');
I = fopen('D:\dsktop\kinect_leap_dataset\acquisitions\P1\G1\1_depth.bin', 'r');
A = fread(I, 480*640, 'uint8=>uint8');
A = reshape(A, 480, 640);
min_row = min(A);
min_col = min(min_row);
for i = 1:480
for j = 1:640
if ((A(i,j) > (min_col + 10)) || (A(i,j) == (min_col + 10)))
image(i,j) = 1;
else
image(i,j) = 0;
end
end
end
imshow(image)
The output I am getting is
Kindly tell what is wrong in this code and why I am not getting any out?
Your code is extremely not vectorize. Here's how to re-write your code in a more vectorize fashion. This is both more efficient and more "readable":
I = fopen('D:\dsktop\kinect_leap_dataset\acquisitions\P1\G1\1_depth.bin', 'r');
A = fread(I, 480*640, 'uint8=>uint8');
A = reshape(A, 480, 640);
min_ = min(A(:)); % minimal value across rows and columns
mask = A>=(min_+10); % no need for loop, vectorize code.
imshow(mask, []);
I'm looking to implement my own Matlab function that can be used to compute image filtering with a 3x3 kernel.
It has to be like this: function [ output_args ] = fFilter( img, mask )
where img is a original image and mask is a kernel (for example B = [1,1,1;1,4,1;1,1,1] )
I'm not supposed to use any in-built functions from Image Processing Toolbox.
I have to use this
where:
s is an image after filter
p is an image before filter
M is a kernel
and N is 1 if sum(sum(M)) == 0 else N = sum(sum(M))
I'm new to MATLAB and this is like black magic for me -_-
This should do the work (Wasn't verified):
function [ mO ] = ImageFilter( mI, mMask )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
numRows = size(mI, 1);
numCols = size(mI, 2);
% Assuming Odd number of Rows / Columns
maskRadius = floor(siez(mMask, 1) / 2);
sumMask = sum(mMask(:));
if(sumMask ~= 0)
mMask(:) = mMask / sumMask;
end
mO = zeros([numRows, numCols]);
for jj = 1:numCols
for ii = 1:numRows
for kk = -maskRadius:maskRadius
nn = kk + 1; %<! Mask Index
colIdx = min(max(1, jj + kk), numCols); %<! Replicate Boundary
for ll = -maskRadius:maskRadius
mm = ll + 1; %<! Mask Index
rowIdx = min(max(1, ii + ll), numRows); %<! Replicate Boundary
mO(ii, jj) = mO(ii, jj) + (mMask(mm, nn) * mI(rowIdx, colIdx));
end
end
end
end
end
The above is classic Correlation (Image Filtering) with Replicate Boundary Condition.
I need to find the coordinates of a 3D sub-image location residing in a 3D image in Matlab. Can anyone help me?
Thanks,
The following code essentially scans through the large array A and compare each element against the first one in the small array B. If an equal is found, a part of A, of which the size is same as of B, is picked out and compared against B.
clear;clc
% reproduce your scenario
A = randi(100, [30, 20, 10]);
B = A(20:30, 1:18, 4:end);
% counter-case verification
% B(end)=200;
% speed up
lenA = numel(A);
[sa1,sa2,sa3] = size(A);
[sb1,sb2,sb3] = size(B);
% a cumbersome method
eqflag = 0;
counterA = 1;
while (counterA <= lenA)
if A(counterA) == B(1)
[subA1,subA2,subA3] = ind2sub([sa1,sa2,sa3],counterA);
if ( (subA1+sb1-1)<=sa1 ) && ( (subA2+sb2-1)<=sa2 ) ...
&& ( (subA3+sb3-1)<=sa3 ) && isequal( B, ...
A(subA1+(1:sb1)-1,subA2+(1:sb2)-1,subA3+(1:sb3)-1) )
eqflag = 1;
break;
end
end
counterA = counterA + 1;
end
if eqflag
fprintf('found matching starting at A(%d, %d, %d).\n', ...
subA1, subA2, subA3);
fprintf('matching region A(%d:%d, %d:%d, %d:%d).\n', ...
subA1, subA1+sb1-1, subA2, subA2+sb2-1, subA3, subA3+sb3-1);
else
fprintf('no matching found.\n');
end
clearvars sa* lenA counterA
% --------------
% a parallel way
[sa1,sa2,sa3] = size(A);
match_first = find(A==B(1));
[m1,m2,m3] = ind2sub([sa1,sa2,sa3],match_first);
region_first_ind = intersect( intersect(find(m1+sb1-1<=sa1), ...
find(m2+sb2-1<=sa2)),find(m3+sb3-1<=sa3)); % array size issue
region_first = num2cell( [m1(region_first_ind),m2(region_first_ind),...
m3(region_first_ind)], 2);
region = cellfun(#(v) [v;v+[sb1,sb2,sb3]-1], region_first, ...
'UniformOutput', false);
region_match = cellfun(#(v) isequal(A(v(1):v(2), v(3):v(4), v(5):v(6)),...
B), region, 'UniformOutput', false);
match = cell2mat(region([region_match{:}]));
if ~isempty(match)
fprintf('found matching starting at A(%d, %d, %d).\n', ...
match(1), match(3), match(5));
fprintf('matching region A(%d:%d, %d:%d, %d:%d).\n', ...
match(1), match(2), match(3), match(4), match(5), match(6));
else
fprintf('no matching found.\n');
end
% obtain small windows of the image (say 16x16 windows and possibly using the crop function)
[rows columns] = size(B);
blockSizeR = 25; % Rows in block.
blockSizeC = 25; % Columns in block.
wholeBlockRows = floor(rows / blockSizeR);
wholeBlockCols = floor(columns / blockSizeC);
image3d = zeros(wholeBlockRows, wholeBlockCols);
sliceNumber = 1;
dataStruct = [];
for row = 1 : blockSizeR : rows
for col = 1 : blockSizeC : columns
row1 = row;
row2 = row1 + blockSizeR - 1;
col1 = col;
col2 = col1 + blockSizeC - 1;
oneBlock = B(row1:row2, col1:col2);
subplot(4, 4, sliceNumber);
imshow(oneBlock);
caption = sprintf('Block #%d of 16', sliceNumber);
title(caption);
drawnow;
dataStruct = [dataStruct, oneBlock(:)];
sliceNumber = sliceNumber + 1;
end
end
I am trying to extract 16 25x25 subwindows from a 100x100 pixel image, then convert each subwindow into a 125 column vector, but my data structure for appending all these vectors seem to be 625 x 16 instead of 125 x 16.
The subwindows seem to be displayed fine in the figure. Any clues as to where i went wrong would be much appreciated.
You can use mat2cell and cellfun:
dataStruct = mat2cell( B, blockSizeR * ones( 1, wholeBlockRows ), ...
blockSizeC * ones( 1, wholeBlockCols ) );
dataStruct = cellfun( #(x) x(:), dataStruct, 'uni', 0 );
dataStruct = [dataStruct{:}];