Group multiple lines to one "object" and be able to "toggle" on or off
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hi, I have this functioj that draws a grid on an image
function drawGrid(RowSize,ColSize,IM,ax)
% Rowsize=512
% Colsize=256
[height,width]=size(IM);
nRows=floor(height/RowSize);
nCols=floor(width/ColSize);
hold(ax,"on");
for i=1:nRows %plot Col Lines
plot(ax,[1 width],[i*RowSize i*RowSize],'r','LineWidth',0.2)
end
for i=1:nCols %Plot Row Lines
XX=i*floor(width/nCols);
plot(ax,[XX XX],[1 height],'r','LineWidth',0.2)
end
hold(ax,"off");
end
I want to be able to toggle the whole grid on or off once its drawn. So I was wondering
1: How to group all the lines to one object?
2: If I pass that "object" as an output, how can I toggle on or off.
Thanks
Jason
채택된 답변
Mathieu NOE
2025년 9월 29일
hello
To group multiple lines into a single "object" in MATLAB, you can use a hggroup or hgtransform object.
here your code updated with hggroup
im = imread('your_image_here.jpg');
figure
imshow(im)
RowSize=40; % whatever number you want
ColSize=40; % whatever number you want
group = drawGrid(RowSize,ColSize,im,gca);
% Toggle visibility of the group
set(group, 'Visible', 'off'); % Hide all lines in the group
% and
set(group, 'Visible', 'on'); % Show all lines in the group
%%%%%%%%%%%%
function group = drawGrid(RowSize,ColSize,IM,ax)
% Rowsize=512
% Colsize=256
[height,width,p]=size(IM);
nRows=floor(height/RowSize);
nCols=floor(width/ColSize);
hold(ax,"on");
% Group the lines into a single object
group = hggroup; % create group
for i=1:nRows %plot Col Lines
prow{i} = plot(ax,[1 width],[i*RowSize i*RowSize],'r','LineWidth',0.2) ;
set(prow{i} , 'Parent', group);
end
for i=1:nCols %Plot Row Lines
XX=i*floor(width/nCols);
pcol{i} = plot(ax,[XX XX],[1 height],'r','LineWidth',0.2);
set(pcol{i} , 'Parent', group);
end
hold(ax,"off");
end
댓글 수: 8
Thats exactly what Im after - I hadn't heard of hggroup before. Thanks
Mathieu NOE
2025년 9월 29일
편집: Mathieu NOE
2025년 9월 29일
Actually, there something not quite right, On my axes "ax" I have an image
as soon as I equate the plot to something (like this)
prow{i} = plot(ax,[1 width],[i*RowSize i*RowSize],'r','LineWidth',0.2) ;
the lines are not plotted on my axes that I pass in i.e.ax, instead a new blank figure opens up.
But if i do this, it does plot on the image which is on axes ax.
plot(ax,[1 width],[i*RowSize i*RowSize],'r','LineWidth',0.2) ;
Why is that?
also would this be an alternative approach
h=findobj('Type','line')
Mathieu NOE
2025년 9월 30일
편집: Mathieu NOE
2025년 9월 30일
which matlab release are you working with ?
I tested my code with R2025a
could you share your code or a working example of your issue ?
Hi, Im using 2024b. Here is my code
function group=drawGrid(RowSize,ColSize,IM,ax)
% IM is the image
% ax is the uiaxes to display the lines on (which also has the image IM)
[height,width]=size(IM);
nRows=floor(height/RowSize);
nCols=floor(width/ColSize);
hold(ax,"on");
% Group the lines into a single object
group = hggroup; % create group
for i=1:nRows %plot Col Lines
prow{i}=plot(ax,[1 width],[i*RowSize i*RowSize],'r','LineWidth',0.2);
set(prow{i} , 'Parent', group);
end
for i=1:nCols %Plot Row Lines
XX=i*floor(width/nCols);
pcol{i}=plot(ax,[XX XX],[1 height],'r','LineWidth',0.2);
set(pcol{i} , 'Parent', group);
end
hold(ax,"off");
end
And this is how I call it
clc;
ax=app.UIAxes; ax2=app.UIAxes2; cla(ax2,"reset"); CL=ax.CLim;
IM=getimage(ax); [sy,sx]=size(IM);
RemoveAnnotations(app,ax); RemoveAnnotations(app,ax2);
xl=400; xr=10650; yt=0; yb=0; % For image crop
n=numel(app.imgArray); % app.imgArray = my images
data=[];
for i=1:n
% Get Image from ImageStack (already created)
app.ImSpinner.Value=i;
data(i,1)=i;
IM=app.imgArray{i}; % This is where my images are!
[sy,sx]=size(IM);
if i==1
cla(ax,"reset");
app.myimage=imagesc(ax, IM); colormap(ax,'gray'); axis(ax,'image'); set(ax,'xtick',[]); set(ax,'ytick',[]);
else
% This is faster to view images by using CData
RemoveAnnotations(app,ax);
set(app.myimage, 'CData', IM); % Update the image data using handle app.myimage (much faster)
end
% Crop Image & autoscale
rect=[xl, yb, xr-xl,sy-yt-yb]; %[x, y, width, height]
hold(ax,'on')
rectangle(ax,'Position',rect, 'EdgeColor','y','LineWidth',1);
IM=imcrop(IM,rect); cla(ax,"reset");
myImagesc(app, ax, IM); cl=[0.15,0.15,0.2]; ax.Color=cl; drawnow;
[sy,sx]=size(IM);
% Create Block array % make the y direction 512 pixels
ngridscol=floor(sx/512);
ngridsrow=floor(sy/512);
ReportMessage(app,[num2str(i),' # of Rows: ',num2str(ngridsrow)]);
% Perform BlockProc Measurements
[blockFwhm,blockFwhm1,blockFwhm2]=IQBlockProc_FOV(app,ax,ax2,ngridsrow,ngridscol,IM,CL);
med=mean2(blockFwhm); mediandev=3*std2(blockFwhm);
data(i,2)=med; data(i,3)=mediandev;
end
where
function RemoveAnnotations(app,ax)
clc;
h1 = findall(ax, 'type', 'line');
h2 = findall(ax, 'type', 'text');
h3 = findall(ax, 'type', 'rectangle');
delete(findall(ax,'type', 'Marker')); %Delete markers
delete(findall(ax,'Type','images.roi.Line'));
h4 = findall(ax, 'type', 'ConstantLine');
delete(h1); delete(h2); delete(h3); delete(h4);
and
function [blockFwhm,blockFwhm1,blockFwhm2]=IQBlockProc_FOV(app,ax,ax2,ngridsrow,ngridscol,IM,CL)
% % % ngridsrow=19; % was 20
% % % ngridscol=19; % Number of blocks in each direction ngridsrow=100; ngridscol=1;
[sy,sx]=size(IM);
RowSize=floor(sy/ngridsrow); ColSize=floor(sx/ngridscol);
app.myGrid=drawGrid(RowSize,ColSize,IM,ax);
blockSizeRow=RowSize;
blockSizeCol=ColSize;
blockImages = splitImageIntoBlocks(IM, blockSizeRow, blockSizeCol);
[nBlocksRow, nBlocksCol] = size(blockImages);
% setup waitbar for info:
hwait = waitbar(0, 'Please wait... ','Position',[10 50 300 50],'Name','Working on Block...','CreateCancelBtn','setappdata(gcbf,''canceling'',1)'); %left, bottom, width, height
l=nBlocksRow*nBlocksCol;
indx=1; ct=0;
xoff=floor(blockSizeCol/2);
yoff=floor(blockSizeRow/2);
blockFwhm = zeros(nBlocksRow, nBlocksCol); % Holder for variable 1
blockFwhm1 = zeros(nBlocksRow, nBlocksCol); % Holder for variable 2
blockFwhm2 = zeros(nBlocksRow, nBlocksCol); % Holder for variable 3
data=[];
midblockCol=round(nBlocksCol/2)+1;
for blockIdxRow = 1 : nBlocksRow
% Check for clicked Cancel button
if getappdata(hwait,'canceling')
break
end
for blockIdxCol = 1 : nBlocksCol
if (blockIdxCol~=1) &&(blockIdxCol~=midblockCol) &&(blockIdxCol~=nBlocksCol) %Just get ends and central regions
indx=indx+1; ct=ct+1;
continue
end
waitbar(indx/l,hwait,[num2str(indx),'/',num2str(l)]);
indx=indx+1; ct=ct+1;
drawnow;
blockImage = double(blockImages{blockIdxRow, blockIdxCol});
blockImage=blockImage-min(blockImage(:)); % Background subtract
try
% Make measurements on BlockImage
sd=std2(blockImage);
FM = std2(blockImage)^2/mean2(blockImage);
Bren=fmeasure(blockImage,'BREN',[]); % Focus metric
[~,ImQS]=IQCalc(app,blockImage,0); % another focus metric
catch
blockFwhm(blockIdxRow, blockIdxCol) = NaN;
blockFwhm1(blockIdxRow, blockIdxCol) = NaN;
blockFwhm2(blockIdxRow, blockIdxCol) = NaN;
continue;
end
if ct==1
cla(ax2,"reset");
app.myimage=imagesc(ax2,blockImage); %image creation
colormap(ax2,'gray'); axis(ax2,'image'); set(ax2,'xtick',[]); set(ax2,'ytick',[]);
ax2.CLim=CL;
else
RemoveAnnotations(app,ax2);
set(app.myimage, 'CData', blockImage); % Update the image data using handle app.myimage (much faster)
end
blockFwhm(blockIdxRow, blockIdxCol) = ImQS;
blockFwhm1(blockIdxRow, blockIdxCol) = sd; %FM;
blockFwhm2(blockIdxRow, blockIdxCol) =Bren; %fwhmBlockY;
% Show current Block location analysed on raw image
xx=blockSizeCol*blockIdxCol-xoff;
yy=blockSizeRow*blockIdxRow-yoff;
hold(ax,"on");
plot(ax,xx,yy,'c.','MarkerSize',20); drawnow;
end
end
delete(hwait);
% Remove all columns with zeros
blockFwhm = blockFwhm(:,any(blockFwhm,1)); blockFwhm1 = blockFwhm1(:,any(blockFwhm1,1)); blockFwhm2 = blockFwhm2(:,any(blockFwhm2,1));
hmm, it's going to be difficult for me to help you
app's are not really my area of expertise and I probably will not be able to reproduce & run your test environment on my side
what is the error message you get ?
Ok thsnks for your help and introducing me to hggrouo.
I think i have an easier way, but have run into another issue, so i will post a new question.
ok good luck !!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
