vectorizing a for loop

조회 수: 2 (최근 30일)
Mutlu
Mutlu 2011년 5월 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
Andrew Newell
Andrew Newell 2011년 5월 30일
It would be easier to answer this question if you showed us your code.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
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.
  댓글 수: 1
Mutlu
Mutlu 2011년 5월 30일
Thank you!!! this worked beautifully. Yes this is exactly what I have positive integers for segment IDs and they always show up in the LUT. Thank you again. Cheers....

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by