Wednesday, 15 April 2015

Replace multiple pixels value in an image with a certain value Matlab -


I have an image 640x480 img , and I do not consider pixels in this list or x = [1, 2, 3, 4, 5] with a fixed value of 10 , so that in any pixel img does not There will be no value in x will be replaced by 10 I already know that it is img (img ~ = 1 & img ~ = 2 & amp; img ~) using only img (img ~ = 1) = 10 = 3 and IMG ~ = 4 and IMG ~ = 5) = 10 but when I img (img ~ = x) = 1 0 then the must agree to the matrix dimensions . So if someone can kindly advise you.

You can easily find it and with combination. We can create a 3D column vector containing elements of [1,2,3,4,5] , then use the bsxfun equals method ( Click on @ a ) so that we can create a 3D matrix of 5 slices. Each piece will tell you whether the locations in the image match an element in No x . The first piece will give you places that no match x = 1 , the second piece will give you places that not match x = 2 , and so on.

Once you have finished it, we can use a call operating on the third dimension to consolidate pixel locations that are not equal to 1, 2, 3, 4 or 5 . The last step would be to take this logical map, which does not tell you a location from 1, 2, 3, 4 or 5 and we set those locations in 10.

One thing we must consider is that image type and vector x should be of the same type. We can ensure that the vector is cut in the same class as img .

Like, do something like this:

  x = sequence ([1 2 3 4 5], [3 1 2]); Vals = bsxfun (@ne, IMG, Cast (X, Class (IMG)); Ind = All (Walls, 3); IMG (Ind) = 10; The advantage of the above method is that the list that you want to use to check the elements, that can be what you want. This prevents dirty logical indexing syntax, such as  img (img ~ = 1 & amp; img ~ = 2 & amp; ....) . All you have to do is change the input list on the starting line of the code, and work for  bsxfun ,  permute  and  any  You should 

Here is an example 5 x 5 image:

  & gt; & Gt; RNG (123,123); & Gt; & Gt; Img = randi (7, 5, 5) img = 3 4 3 6 5 7 2 6 5 1 3 1 6 1 7 6 4 4 3 3 6 2 4 1 3 Use of  

By the above code, the output we get is:

  img = 3 4 3 10 5 10 2 10 5 1 3 1 10 1 10 10 4 4 3 3 10 2 4 1 3  

You can certainly see that those elements are set to 10, not 1, 2, 3, 4 or 5.

on one side

If you do not like the permute and bsxfun approach, for a loop in a way And initially, put all true array, logical and end result with a logical map that is in the place that is located in x The values ​​are not equal. Finally, we have a logical map where true are places that are not equal to 1, 2, 3, 4 or 5.

So, do something like this:

  Ind = true (size (IMG)); For IDX = 1: 5 Ind = Ind & amp; Img ~ idx; End IMG (Ind) = 10;  

If you do this instead, you will see that we get the same answer.


No comments:

Post a Comment