Python Forum
Odd numpy error with subtraction
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Odd numpy error with subtraction
#5
(May-30-2020, 11:25 PM)scidam Wrote: Ok. find_closest expects that rgb has shape (m, n) and color has shape (n,). In our case n = 3. The only way here is to investigate why rgb has shape (xxxx, ), i.e. why it is one dimensional array (it should be 2D array). Could you please provide minimal reproducible example here that leads to the problem?
Here's a very simplified bit of code. You will need a folder with some images (no more than 5 will work):
import numpy as np
import cv2
import glob

rgb, links = [], []

def find_closest(color, rgb):
    return rgb[np.argmin(np.abs(rgb - color).sum(axis=1))]

def get_average(image, path):       
	avg_col_per_row = np.average(image, axis=0)
	avg = np.average(avg_col_per_row, axis=0)

	rgb.append(avg.astype(int).tolist())
	links.append(path)

for l in glob.glob("your_image_path"):
	get_average(cv2.imread(l, cv2.IMREAD_UNCHANGED), str(l))

some_colour = [100,123,14]
print(find_closest(some_colour, np.array(rgb)))
In theory, this should make rgb a 2D array since it's an array, full of arrays with 3 integers inside.
Reply


Messages In This Thread
RE: Odd numpy error with subtraction - by scidam - May-30-2020, 12:44 PM
RE: Odd numpy error with subtraction - by scidam - May-30-2020, 11:25 PM
RE: Odd numpy error with subtraction - by DreamingInsanity - May-31-2020, 08:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Numpy error while filling up matrix with Characters august 4 1,928 Apr-13-2022, 10:28 PM
Last Post: august
  forloop to compute sum by alternating from addition to subtraction JulianZ 3 1,889 Apr-02-2022, 09:36 AM
Last Post: DeaD_EyE
Bug Error while importing numpy Erfan 3 3,335 Nov-28-2020, 07:49 AM
Last Post: bowlofred
  Error in importing numpy fullstop 1 1,965 Dec-18-2019, 12:07 PM
Last Post: jefsummers
  Basic subtraction error rix 4 3,469 Oct-11-2019, 06:43 AM
Last Post: buran
  How to make a subtraction within a range of numbers? Alberto 3 10,182 May-08-2017, 09:13 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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