zzzz
zzzz

Reputation: 73

to make the image pixels easier to access in R

I have created a function in R which is taking a lot of time to execute.If possible please suggest some alternative to do this.

totest<-function(img)
{ 

for(k in 1:130)
    for(i in 1:130)
        for(j in 1:130)
            if(img[i,j,k]<0)
                img[i,j,k]<-(-a[i,j,k])
}

I am passing an image with dimension 130*130 to the function totest which is checking each pixel for -ve value and if the value is -ve then it converts it into +ve.

Upvotes: 1

Views: 50

Answers (1)

josliber
josliber

Reputation: 44330

You can identify all the indices to replace and then copy them over in a single operation:

indices <- img < 0
img[indices] <- -a[indices]

Upvotes: 2

Related Questions