0
$\begingroup$

I have a greyscale png image and want to produce a list of all pixel values which are not 1 for analyzing them further.

image:

enter image description here

My code is the following:

image = Import["https://i.stack.imgur.com//SC0F3.png"];

RepeatedTiming[
 imageData = ImageData[image]; 
 imageDataWithout1 = DeleteCases[Flatten@imageData, value_ /; value == 1];
 ]
 {0.13, Null}

(*imageDataWithout1 is the resulting list *)

Check:

Dimensions[Flatten@imageData] - Dimensions[imageDataWithout1]

{34323}

Count[Flatten@imageData, value_ /; value == 1]

34323

Can this also be done different, since the code is relatively slow?

$\endgroup$
3
$\begingroup$

On my machine Pick seems to be way faster:

image = Import["https://i.stack.imgur.com//SC0F3.png"];
RepeatedTiming[imageData = ImageData[image];
 imageDataWithout1 = 
  DeleteCases[Flatten@imageData, value_ /; value == 1];]
imageDataWithout1 // Dimensions
(* {0.132, Null}
{253007} *)

RepeatedTiming[imageData = ImageData[image];
 imageDataWithout1 = 
  Pick[#, UnitStep[# - 1.0], 0] &@Flatten@imageData;]
imageDataWithout1 // Dimensions
(* {0.0058, Null}
{253007} *)
$\endgroup$
  • $\begingroup$ Why is this so fast? ... incredible ... thanks $\endgroup$ – mrz Mar 9 '17 at 23:07
  • 1
    $\begingroup$ @mrz I think DeleteCases and Select apply your test function sequentially. UnitStep is Listable so it applies UnitStep[# - 1.0] to a list more effectively. $\endgroup$ – BlacKow Mar 10 '17 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.