vectorizing a for loop
조회 수: 2 (최근 30일)
이전 댓글 표시
Hell all - I know that this question has been asked many times before but I feel like my problem is slightly different and is bigger (computationally). So here it goes. Any help would be much appreciated. Thank you - Mutlu..
I have an image with segments. The image size is 7000 x 7000 and contains roughly 1 million segments. Multiple pixels make up segments of various sizes and the image contains the unique segment IDs for each segment. I also have a look up table that associates each segment ID to a specific code (it is a two column matrix Segment ID and code). Using this look up table, I would like to assign each segment its appropriate code from the table. I tried doing this in a for loop where I would go through each segment ID, find the pixels that belong to that segment and assign the code from the look up table. Unfortunately, this is painfully slow for this large image. Are there easier - vectorized - ways of doing this? Of course there may be built in tools in the image processing toolbox (which I have) that may be helpful as well but I am not familiar with that toolbox.
댓글 수: 1
채택된 답변
Walter Roberson
2011년 5월 30일
The image is itself composed of segment IDs? Are the IDs nonnegative integers? If so then that would correspond to a "labeled image" in MATLAB.
[tf, idx] = ismember(LabeledImage, LookupTable(:,1));
CodedImage = LookupTable(idx,2);
This will work even if the segment IDs are not nonnegative integers, but if the segment IDs contain fractions you would need to be careful.
This code does require that every value in the labeled image appears somewhere in the lookup table.
In some situations, the code can be made more efficient.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!