Python Forum
Still struggling with np.where...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Still struggling with np.where...
#1
Hello everyone

Despite some help on this forum I am still struggling with np.where

Can someone help solve my problem?

I have a numpy array derived from an image which is 1024 x 1024 x 4 in size. eg [10,11,0] is the red attribute of the pixel located at x axis 11 y axis 10 (yes its reversed).

Lets assume that my array has various values all less than 253 in all the array except for the locations 10,11 and 23,24 and 83,89. I want to use np.where to find the locations of pixels where [x,y,0]>252.

How can I do this using np.where ?
The output I want to generate is a list of positions i.e [[10,11], [23,24],[83,89]} so it may be necessary to convert tuple into a list and then swap the x and y axis.

I have tried using
new_list = img[np.where(img[:, :][0] >252)]
I get the following error
Output:
TypeError: 'builtin_function_or_method' object is not subscriptable
but this is not working for me.
Thanks Peter
Reply
#2
np.where returns lists of indices where the condition is met. So, if you correctly apply np.where to your data, you get something like this: (array([10, 23, 83]), array([11, 24, 89])). The latter could be converted to [[10, 11], [23, 24]...] using zip:

list(zip(*np.where(img[..., 0]>252)))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Struggling with Juggling JSON Data SamWatt 7 1,820 May-09-2022, 02:49 AM
Last Post: snippsat
  Syntax errors: Struggling to setup enviroment and load packages AH56 5 2,724 Jun-30-2021, 01:01 PM
Last Post: AH56
  Struggling for the past hour to define function and call it back godlyredwall 2 2,158 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  struggling with != statements CallumRoberts2004 2 1,509 Aug-18-2020, 03:01 PM
Last Post: GOTO10
  I’m Flat out struggling to understand list indexes gr3yali3n 7 2,854 Jul-20-2020, 07:18 PM
Last Post: princetonits
  Struggling with nested list gr3yali3n 3 2,257 Jul-09-2020, 05:30 PM
Last Post: DPaul
  Struggling to exit this while loop fatherted99 5 2,412 Feb-08-2020, 07:46 PM
Last Post: fatherted99
  Struggling with several while loops nsadams87xx 1 1,791 Nov-25-2019, 02:12 AM
Last Post: Larz60+
  struggling with loop/webscrape zarize 4 2,441 Sep-27-2019, 08:44 AM
Last Post: zarize
  Struggling To Work With Anti-Captcha API digitalmatic7 1 5,682 Oct-09-2017, 09:51 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020