이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I'm working on a GUI that outputs user commands to a script file, and I've run into an issue programming the structuring element for performing morphological operations. As it stands, I construct the structuring element by allowing the user to choose the shape and the values for radius, length, or whatever, so the structuring element looks something like:
se = strel('disk',5);
However, I can't output that statement to the script using fprintf because it's a mix of character values and numerical values. So, how can this be done?
채택된 답변
fid = fopen ('script.txt', 'a') ;
disk = 5 ; % Defined form user input.
fprintf(fid, 'se = strel(''disk'', %g);\n', disk) ;
square = 7 ; % Defined form user input.
fprintf(fid, 'se = strel(''square'', %g);\n', square) ;
% You could also have the shape given as a string from some drop-down and
% use it as in ..
shape = 'triangle' ; % Defined from drop-down.
value = 5 ;
fprintf(fid, 'se = strel(''%s'', %g);\n', shape, value) ;
% ...
fclose(fid) ;
댓글 수: 9
I'm looking for some way to input the structuring element all together. The way my code is written, the user selects the shape from a button group and the size from sliders. That is then placed in the se() variable. However, I can't use that se() in the fprintf function.
fprintf(fid,'img = imdilate(img, %g); \n', se)
gives an error because you can't output a structuring element like this; is there another way to do it that is less labor intensive than what you've proposed?
Cedric
2013년 3월 18일
But it is you who create the STREL from some UI call back, no? At the moment of its creation, you could also build the string as mentioned above. Or do you receive directly a STREL object from an UI that you cannot modify/understand?
The structuring element is created once the user sets its parameters. I then use those parameters and apply that operation to the image. The strel() function allows for multiple numeric inputs, so I can't simply break the strel command into its constituent shape and value parts and do:
fprintf(fid, 'se = strel(''%s'', %g);\n, shape, value)
in the case that I have more than one value to gather for a particular shape.
The syntax is
SE = strel(shape, parameters) ;
with a variable number of parameters ranging from 1 to 3 (for 'ball').
There is a point in your code where you make this call, and then you translate the strel or you perform a series of other operations. At this point, you get the shape and relevant parameters from the interface.
I don't understand what prevents you from building a string which corresponds to the relevant call at this moment; you have probably already a switch whose cases select the right type of call. The only tricky situation is if you have a mechanism in the interface for managing 'arbitrary' shapes; in such case, you would have to store the array as well.
I had a look at your former question here. Note that you could build methods for managing logs, e.g.
properties
log_fid = -1
...
end
methods
function self = logOpen(self, fileLocator)
self.log_fid = fopen(fileLocator, 'w') ;
end
function logClose()
fclose(self.log_fid) ;
end
function log(self, fmt, varargin)
if self.log_fid > 0
fprintf(self.log_fid, fmt, varargin{:}) ;
end
% or throw an error/warning
end
function SE = strel(self, shape, varargin)
if self.log_fid > 0
self.log('%%Build STREL (shape=%s)\n', shape) ;
cmdStr = ['SE = strel(''', shape, '''', ...
sprintf(', %g', varargin{:}), ')\n'] ;
self.log(cmdStr) ;
end
SE = strel(shape, varargin{:}) ;
end
end
I am mentioning this to illustrate the use of varargin read as a comma -separated list.
EDIT: added self.strel() so you see how you can have a STREL function adding automatically an entry into the logs.
Is the issue that you don't know how to print the apostrophe to the text file?
The issue is that the following command doesn't work
se = strel('disk',5);
fprintf(fid, 'img = imdilate(img, %s); \n, se);
And I guess that's due partly to the syntax being incorrect because 'disk' needs double quotes around it in the fprintf() function. I was just trying to find a simple way to maybe decompose the structuring element to separate shape and parameter in order to rebuild the fprintf() command correctly.
Actually if you perform
se = strel('disk',5);
img = imdilate(img, se);
what you want to do in order to save these "commands" to file is:
fprintf(fid, 'se = strel(''disk'',5);\n') ;
fprintf(fid, 'img = imdilate(img, se);\n') ;
If you had the shape defined by some control in your GUI and saved in a string variable named shape, you could do the following:
fprintf(fid, 'se = strel(''%s'',5);\n', shape) ;
fprintf(fid, 'img = imdilate(img, se);\n') ;
In any case, you want the string 'se' to be in the format of FPRINTF, and not the output of disp(se), as it is the se that will be created when the saved script will be run that must be passed to IMDILATE, and not "some representation" of the current se.
Caleb
2013년 3월 19일
That will probably work. Good Call.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Object Detection에 대해 자세히 알아보기
참고 항목
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)
