hellonico / origami

Lowest barrier of entry to Image Processing, Computer Vision and Neural Networks on the JavaVM
https://hellonico.github.io/origami-docs/#/
Eclipse Public License 1.0
122 stars 7 forks source link

cannot figure out how to multiply or divide a channel by a scalar in order to renormalize it #20

Closed Chouffe closed 5 years ago

Chouffe commented 5 years ago

Using origami with MXNet, I need to be able to renormalize a channel color by a scalar before feeding the image into my networks. I looked up examples in Python and they often rely on numpy to perform such an operation: http://akash0x53.github.io/blog/2013/04/29/RGB-Normalization/

Is there a way to achieve this with origami? I have tried using cv/multiply and cv/divide without success...

hellonico commented 5 years ago

Yes, origami has a function for that but getting terrible results due to black pixels.

In the formula from the link you sent, I don't really get the divide by 0 in case the sum of R+G+B on a black pixel (0,0,0) gives a sum of 0.

Is it suppose to be 0 ? or something else ?

Chouffe commented 5 years ago

Yes the example above is broken for black pixels. And I also do not care about the scaling factor in his example. In my case, total would be the standard deviation of the color channel values across the image dataset. So it will never be black.

hellonico commented 5 years ago

When the channels are independent you can use the following:

(deftest multiply
    (let [m (cv/new-mat 2 2 cv/CV_8UC3 (cv/new-scalar 10 20 30))]
     (cv/multiply! m (u/matrix-to-mat-of-double [[1.0 0.0 0.0]]))
     (is (= (cv/->string m)
        [[10 0 0 10 0 0]
         [10 0 0 10 0 0]]))))

(deftest divide
    (let [m (cv/new-mat 2 2 cv/CV_8UC3 (cv/new-scalar 10 20 30))]
     (cv/multiply! m (u/matrix-to-mat-of-double [[1/5 1/10 1/30]]))
     (is (= (cv/->string m)
        [[2 2 1 2 2 1]
         [2 2 1 2 2 1]]))))

(deftest divide-bikkuri 
        (let [m (cv/new-mat 2 2 cv/CV_8UC3 (cv/new-scalar 10 20 30))]
     (cv/divide! m (cv/new-scalar 10.0 20.0 30.0))
     (is (= (cv/->string m)
        [[1 1 1 1 1 1]
         [1 1 1 1 1 1]]))))
hellonico commented 5 years ago

Added related example to channel transformation using functions or matrixes: https://github.com/hellonico/origami/blob/a2c855a1138dd61cebc8303b7008c5aec370dcec/test/origami/channels_test.clj