8 views (last 30 days)

Show older comments

Hello, I want to show mask one quadrant by one quadrant and take images. But the code is making on one quadrant and the two and three...Eventually all the quadrants are on. After comparing the intensities of the image, I want to move to that quadrant and divide it again by four, such as in images pdf attached. Can anyone please help me with this?

% function dimension = getquadrant(q1,q2,q3,q4)

clc

clear all

close all

%%load dmd

classname = 'alpV42x64';

tagname = classname;

pseudoDLL = logical(0);

protofile = 'alpV42x64proto.m';

protofunc = @alpV42x64proto;

arch = 'win64';

dllfile = 'C:\Users\ReddyLabAdmin\Documents\MATLAB\alp4395.dll';

alplib('delete')

alplib('set', 'tag', tagname, 'classname', classname, ...

'dllfile', dllfile, 'pseudoDLL', pseudoDLL, 'protofile', protofile, ...

'protofunc', protofunc, 'arch', arch)

myapi = alpload();

mydev = alpdevice(myapi);

mydev.alloc();

%define the size of dmd

i = 1024;

j = 768;

dmd = zeros(i,j,'uint8');

[rows, columns, numColorChannels] = size(dmd)

numblocks = 2;

topRows = round(linspace(1, rows+1, numblocks + 1))

leftColumns = round(linspace(1, columns+1, numblocks + 1))

%show mask and capture image

for row = 1 : length(topRows) - 1

row1 = topRows(row);

row2 = topRows(row + 1) - 1;

for col = 1 : length(leftColumns) - 1

col1 = leftColumns(col);

col2 = leftColumns(col + 1) - 1;

dmd(row1 : row2, col1 : col2, :) = 255;

mydev.put(dmd);

myimage1 = captureimage();

%imwrite(myimage1,'r_row%d%dcolumn%d%d.jpg,row1, row2, col1, col2');

mydev.stop();

end

end

% %q1

% dmd = zeros(i,j,'uint8');

% q1 = 1:j/2,1:i/2;

% dmd(q1) = 255;

% mydev.put(dmd);

% myimage1 = captureimage();

% imwrite(myimage1,'Q1.jpg');

% mydev.stop();

% %q2

% dmd = zeros(i,j,'uint8');

% q2 = j/2:j,1:i/2;

% dmd(q2) = 255;

% mydev.put(dmd);

% myimage2 = captureimage();

% imwrite(myimage2,'Q2.jpg');

% mydev.stop();

% %q3

% dmd = zeros(i,j,'uint8');

% q3 = 1:j/2,i/2:i;

% dmd(q3) = 255;

% mydev.put(dmd);

% myimage3 = captureimage();

% imwrite(myimage3,'Q3.jpg');

% mydev.stop();

% %q4

% dmd = zeros(i,j,'uint8');

% q4 = j/2:j,i/2:i;

% dmd(q4) = 255;

% mydev.put(dmd);

% myimage4 = captureimage();

% imwrite(myimage4,'Q4.jpg');

% mydev.stop();

maximumintensity = calculatemaximum(myimage1,myimage2,myimage3,myimage4);

% if maximumintensity == meanIntensityValue1

% dimension = size(q1);

% else if maximumintensity == meanIntensityValue2

% dimension = size(q2);

%

% else if maximumintensity == meanIntensityValue3

% dimension = size(q3);

%

% else maximumintensity == meanIntensityValue4

% dimension = size(q4);

% end

DGM
on 10 Oct 2021

Edited: DGM
on 10 Oct 2021

This should be a start

s = [768 1024];

% generate three masks that can be transformed and combined

% to create the desired masks

m4 = false(s);

m4(1:s(1)/2,1:s(2)/2) = true;

squaresize = s/4;

xx = mod(0:(s(2)-1),squaresize(2)*2)<squaresize(2);

yy = mod(0:(s(1)-1),squaresize(1)*2)<squaresize(1);

m16 = xx & yy';

squaresize = s/8;

xx = mod(0:(s(2)-1),squaresize(2)*2)<squaresize(2);

yy = mod(0:(s(1)-1),squaresize(1)*2)<squaresize(1);

m64 = xx & yy';

% just display them for sake clarifying the example

subplot(1,3,1)

imshow(m4)

subplot(1,3,2)

imshow(m16)

subplot(1,3,3)

imshow(m64)

This is an example of how this can be used to generate all the masks:

clf

for a = 1:4

thismask = xfmask(m4,a);

% do something with the mask ...

dothings(thismask);

end

for a = 1:4

tm4 = xfmask(m4,a);

for b = 1:4

thismask = tm4 & xfmask(m16,b);

% do something with the mask ...

dothings(thismask);

end

end

for a = 1:4

tm4 = xfmask(m4,a);

for b = 1:4

tm16 = xfmask(m16,b);

for c = 1:4

thismask = tm4 & tm16 & xfmask(m64,c);

% do something with the mask ...

dothings(thismask);

end

end

end

% placeholder task function

function dothings(m)

imshow(m)

pause(0.1)

end

% convenience function to flip masks

function y = xfmask(x,k)

switch k

case 1

y = x;

case 2

y = fliplr(x);

case 3

y = flipud(x);

case 4

y = rot90(x,2);

otherwise

error('k must be between 1 and 4')

end

end

This will just show the masks in sequence. Bear in mind that if the equipment controller is expecting a uint8-scaled image, you can just use im2uint8(thismask) to convert it.

I bet there's some elegant way of doing this, but this works.

DGM
on 12 Oct 2021

Oh I missed that. Maybe this is a bit closer.

s = [768 1024];

% generate three masks that can be transformed and combined

% to create the desired masks

m4 = false(s);

m4(1:s(1)/2,1:s(2)/2) = true;

squaresize = s/4;

xx = mod(0:(s(2)-1),squaresize(2)*2)<squaresize(2);

yy = mod(0:(s(1)-1),squaresize(1)*2)<squaresize(1);

m16 = xx & yy';

squaresize = s/8;

xx = mod(0:(s(2)-1),squaresize(2)*2)<squaresize(2);

yy = mod(0:(s(1)-1),squaresize(1)*2)<squaresize(1);

m64 = xx & yy';

% build masks and test

intvector = zeros(4,1);

for a = 1:4

thismask = xfmask(m4,a);

% do something with the mask ...

intvector(a) = dothings2(thismask);

end

[maxinta maxidxa] = max(intvector); % index of the image with highest intensity

tm4 = xfmask(m4,maxidxa);

for b = 1:4

thismask = tm4 & xfmask(m16,b);

% do something with the mask ...

intvector(b) = dothings2(thismask);

end

[maxintb maxidxb] = max(intvector);

tm16 = xfmask(m16,maxidxb);

for c = 1:4

thismask = tm4 & tm16 & xfmask(m64,c);

% do something with the mask ...

intvector(c) = dothings2(thismask);

end

[maxintc maxidxc] = max(intvector); % if this value is still needed

% placeholder task function

function fint = dothings2(m)

% this is just visualization for demonstration

imshow(1-m)

pause(0.1)

% other tasks would be done here, for example:

% clear the DMD (if that's necessary)

% set DMD to the current mask

% capture the image

% get image intensity

fint = rand(1); % placeholder intensity for this frame

end

% convenience function to flip masks

function y = xfmask(x,k)

switch k

case 1

y = x;

case 2

y = fliplr(x);

case 3

y = flipud(x);

case 4

y = rot90(x,2);

otherwise

error('k must be between 1 and 4')

end

end

Of course, this isn't complete. I don't have the hardware, so I'm not inclined to write the code for the parts I can't test. I'm not sure why it seems like your masks keep adding up. Does the DMD need to be explicitly cleared?

I'm not sure what the outputs need to be. So far, all this does is generate masks and compare intensities. None of the captured images are saved, only the intensities and indices of the three frames with highest intensity. The indexing scheme is [NW NE SW SE].

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!