image quadrant one by one and move to next quadrant

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

2 Comments

DGM
DGM on 9 Oct 2021
Edited: DGM on 9 Oct 2021
What should happen when the image geometry is not integer-divisible by [8 8]?
EDIT: Or is the image always the 256x256 DMD mask?
I just want the mask(1024*768) to be divided in quadrant and it is divisible up to 256*256.

Sign in to comment.

 Accepted Answer

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.

10 Comments

Thank you.But my problem was to divide the masks into four quadrant only by moving to highest brightness. My masks were being showed one quadrant white, then two quadrant white etc. But I want them one at a time only. Such as in the image attached.
Sorry, this is actually nice. I understand now. I am going to try this one.
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].
Hello DGM, I finaly need the index of a single pixel, which I have to use later. With this code, I get the number of segment and I can manually calculate the center pixel. From this code, I get 12 pixel left and I tried to turn on those pixels and get the maximum intensity again. But, can you help me with directly getting the index of the pixel, which has the highest intensity. Again, it is a 1024X768 array and I attached the working code here.
Sorry about the delay.
If I understand your intent, you should be able to work with something like this:
C = regionprops(thismask,'centroid'); % find the center of the selected mask
C = floor(C.Centroid)
thisstripemask = false(s); % create a new mask
thisstripemask(C(2),C(1)-(0:11)) = true; % with a 12px stripe from the center
At which point you could apply the new mask as needed.
Bear in mind that since the mask region geometry is even, there is no center pixel. This stripe will always be off by half a pixel. This might not matter at all, depending on your needs.
This way I am getting the number of coordinate but not the dimension of the center finally. I need the dimension of the one pixel which is the brightest. Such as my center is right now 515:516,374:375. I have to have them directly by running the code.
I don't understand. Are you trying to place the stripe on the intensity peak within the masked region?
I think we're mixing up "geometric center" and "intensity peak" in our conversation.
It is not geometric center, it will move to the highest intensity pixel like before. But I need the coordinate value(pixel location) rather than the number of the coordinate block. Like the index of m256 in the code.
I got it this way (attached image)
Oh. So the intensity peak within the mask. ... but is the rest of it doing what you needed?

Sign in to comment.

More Answers (0)

Asked:

on 9 Oct 2021

Commented:

DGM
on 4 Mar 2022

Community Treasure Hunt

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

Start Hunting!