Checking if pixel belongs to an image - image

I have written the following function that find if a pixel belongs to an image in matlab.
At the beginning, I wanted to test it as if a number in a set belongs to a vector like the following:
function traverse_pixels(img)
for i:1:length(img)
c(i) = img(i)
end
But, when I run the following commands for example, I get the error shown at the end:
>> A = [ 34 565 456 535 34 54 5 5 4532 434 2345 234 32332434];
>> traverse_pixels(A);
??? Error: File: traverse_pixels.m Line: 2 Column: 6
Unexpected MATLAB operator.
Why is that? How can I fix the problem?
Thanks.

There is a syntax error in the head of your for loop, it's supposed to be:
for i = 1:length(img)
Also, to check if an array contains a specific value you could use:
A = [1 2 3]
if sum(A==2)>0
disp('there is at least one 2 in A')
end
This should be faster since no for loop is included.

for i = 1:length(image)
silly error, not : , it is =

Related

How to write a 3D image from a 3x3 matrix written in fortran 90?

I'm trying to write a 3D image in fortran 90.
The code for the object I want in the image:
Here is a code of a cube in fortran:
PROGRAM myimage
integer xmax,ymax,zmax
parameter (xmax=10,ymax=10,zmax=10)
INTEGER mytable(1:xmax,1:ymax,1:zmax)
do 1 i1=1,xmax
do 2 i2=1,ymax
do 3 i3=1,zmax
mytable(i1,i2,i3)=0
if ((i1.ge.3).and.(i1.le.6).and.(i2.ge.3).and.(i2.le.6).and.(i3.ge.3).and.(i3.le.6)) then
mytable(i1,i2,i3)=1
endif
3 continue
2 continue
1 continue
end
The type of image I'd like to get:
The type of image I want is like this :
The cube would be my pixels mytable=1 and around it there would be pixels : mytable=0
What I tried:
I first tried to write a code to make the image directly in fortran, but it turned out that the image issued was not a 3D image as I wanted (see Appendix 1).
The question:
Could you explain me how to view that type of object in 3D please ?
For instance, following the comment of Vladimir F, I downloaded Paraview.
I found this question that is quite similar to where I stand now.
But I don't understand what exactly I have to write in the file, if I choose to write it in the format UCD. I didn't find explanations on the internet and the link that is brought in the question there does not work.
Appendix 1:
Here is a code for a 2D image and for the 3D image I tried to code.
I first wrote a 2D image that works. I tried to generalize it to 3D. I'd like to view the object where mytable=1 in 3D.
subroutine image2d(mytable,xmax,ymax,zmax)
integer xmax,ymax,zmax,mytable(1:xmax,1:ymax,1:zmax)
character*15 fname
WRITE(fname,'(a)')'myimage2d.ppm'
open (100,file=fname,form='formatted')
write(100,'(a)') 'P3'
write(100,*) '#'
write(100,*) xmax,ymax
write(100,*) 2
do 10 i10=1,xmax
do 20 i20=1,ymax
if (mytable(i10,i20,5).eq.0) then
write(100,*) '2 2 2'
else if (mytable(i10,i20,5).eq.1) then
write(100,*) '0 0 0'
end if
20 continue
10 continue
end
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine image3d(mytable,xmax,ymax,zmax)
integer xmax,ymax,zmax,mytable(1:xmax,1:ymax,1:zmax)
character*15 fname
WRITE(fname,'(a)')'myimage3d.ppm'
open (200,file=fname,form='formatted')
write(200,'(a)') 'P3'
write(200,*) '#'
write(200,*) xmax,ymax,zmax
write(200,*) 3
do 11 i10=1,xmax
do 21 i20=1,ymax
do 31 i30=1,ymax
if (mytable(i10,i20,i30).eq.0) then
write(200,*) '2 2 2'
else if (mytable(i10,i20,i30).eq.1) then
write(200,*) '0 1 2'
end if
31 continue
21 continue
11 continue
end

High & Low Numbers From A String (Ruby)

Good evening,
I'm trying to solve a problem on Codewars:
In this little assignment you are given a string of space separated numbers, and have to return the highest and lowest number.
Example:
high_and_low("1 2 3 4 5") # return "5 1"
high_and_low("1 2 -3 4 5") # return "5 -3"
high_and_low("1 9 3 4 -5") # return "9 -5"
Notes:
All numbers are valid Int32, no need to validate them.
There will always be at least one number in the input string.
Output string must be two numbers separated by a single space, and highest number is first.
I came up with the following solution however I cannot figure out why the method is only returning "542" and not "-214 542". I also tried using #at, #shift and #pop, with the same result.
Is there something I am missing? I hope someone can point me in the right direction. I would like to understand why this is happening.
def high_and_low(numbers)
numberArray = numbers.split(/\s/).map(&:to_i).sort
numberArray[-1]
numberArray[0]
end
high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6")
EDIT
I also tried this and receive a failed test "Nil":
def high_and_low(numbers)
numberArray = numbers.split(/\s/).map(&:to_i).sort
puts "#{numberArray[-1]}" + " " + "#{numberArray[0]}"
end
When omitting the return statement, a function will only return the result of the last expression within its body. To return both as an Array write:
def high_and_low(numbers)
numberArray = numbers.split(/\s/).map(&:to_i).sort
return numberArray[0], numberArray[-1]
end
puts high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6")
# => [-214, 542]
Using sort would be inefficient for big arrays. Instead, use Enumerable#minmax:
numbers.split.map(&:to_i).minmax
# => [-214, 542]
Or use Enumerable#minmax_by if you like result to remain strings:
numbers.split.minmax_by(&:to_i)
# => ["-214", "542"]

ArgumentError: In `load': marshal data too short

I want to realize multiple processes. I have to send the data which bubble-sorted in different child processes back to parent process then merge data. This is part of my code:
rd1,wt1 = IO.pipe # reader & writer
pid1 = fork {
rd1.close
numbers = Marshal.load(Marshal.dump(copylist[0,p]))
bubble_sort(numbers)
sList[0] = numbers.clone
wt1.write Marshal.dump(sList[0])
Process.exit!(true)
}
Process.waitpid(pid1)
Process.waitpid(pid2)
wt1.close
wt2.close
pid5 = fork {
rd5.close
a = Marshal.load(rd1.gets)
b = Marshal.load(rd2.gets)
mList[0] = merge( a,b).clone
wt5.write Marshal.dump(mList[0])
Process.exit!(true)
}
There are pid1...pid7, rd1...rd7, wt1...wt7. pid1...pid4 are bubble-sort 4 part of data. pid5 and 6 merge data from pid1, 2 and pid 3, 4. Finally, pid7 merges the data from pid5 and 6.
When data size is small, it succeeds, but when I input larger data (10000):
Data example : 121 45 73 89 11 452 515 32 1 99 4 88 41 53 159 482 2013 2 ...
then, errors occur: :in 'load': marshal data too short (ArgumentError) and another kind error: in 'load': instance of IO needed (TypeError). The first error line is in pid5: a = ... and pid6: b = .... The other kind of error line is in pid7: b = .... Are my data too big for this method?
Marshal.load and Marshal.dump work with binary data. The problem with the short reads is here:
a = Marshal.load(rd1.gets)
b = Marshal.load(rd2.gets)
#gets reads up to a new-line (or end of file) and then stops. The trouble is that new-line may be present in the binary data created by Marshal.dump.
Change gets to read in both lines.

Confusing result of Matlab's size() function

Very simple issue here I suspect, I am completely new to Matlab (1st day) and have been unable to resolve this by consulting the documentation.
As part of a larger problem, I need to get the dimensions of an image into two variables (row_obj and col_obj for the image object).
According to the documentation:
[m,n] = size(obj)
m: The number of rows in obj.
n: The number of columns in obj.
So, following that, I wrote:
[row_obj, col_obj] = size(object);
disp(row_obj);
disp(col_obj);
disp(size(object));
Which produced the output:
> >> call_i_spy
> 21
>
> 81
>
> 21 27 3
It appears row_obj is correct. If disp(size(object)) produces 21 27 3, then why is col_obj not 27 (the value I need)? What is [row_obj, col_obj] = size(object); actually doing?

reading in a text file with a SUB (1a) (Control-Z) character in R on Windows

Following on from my query last week reading badly formed csv in R - mismatched quotes, these same CSV files also have embedded control characters such as the ASCII Substitute Character which is decimal 26 or 0x1A. Unfortunately readLines() seems to truncate the line at this character, so I am having difficulty in matching quotes - apart from losing the later fields in these lines!
I have tried to readBin() but I can't get it to read this file. I'm afraid I can't cleanly read this into R to give you an example and I'm having difficulty in creating these in R. Sorry not to be able to demonstrate with a clean example. Thoughts?
Update
Now I'm confused - when I use the code
h3 <- paste('1,34,44.4,"', rawToChar(as.raw(c(as.integer(k1), 26, 65))), '",99')
identical(readLines(textConnection(h3)), h3)
I get TRUE which I find quite surprising!
Update 2
h3
[1] "1,34,44.4,\" HIJK\032A \",99"
> writeLines(h3, 'h3.txt')
> h3a <- readLines('h3.txt')
Warning message:
In readLines("h3.txt") : incomplete final line found on 'h3.txt'
> h3a
[1] "1,34,44.4,\" HIJK"
So readLines() reacts differently when coming from a textConnection() and it silently truncates at the SUB character.
I would be surprised if it makes a difference but I'm on 2.15.2 on Windows-64.
Update 3
Some vague success in solving this...
zb <- file('h3.txt', "rb")
tmp <- readBin(zb, raw(), size=1, n=400) # raw is always of size =1
nchar(tmp)
# [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
close(zb)
tmp
# [1] 31 2c 33 34 2c 34 34 2e 34 2c 22 20 48 49 4a 4b 1a 41 20 22 2c 39 39 0d 0a
rawToChar(tmp)
# [1] "1,34,44.4,\" HIJK\032A \",99\r\n"
i.e. if I read in the file as binary and convert to character() afterwards it seems to work... this will be tedious for large CSV files...
Could there be a bug in R in incorrectly detecting a Control-Z as end of file on windows??
I think I've figured out a solution - because there appears to be a problem reading a Control-Z in the middle of a file on Windows, we need to read the file in binary / raw mode.
fnam <- 'h3.txt'
tmp.bin <- readBin(fnam, raw(), size=1, n=max(2*file.info(dfnam)$size, 100))=1
tmp.char <- rawToChar(tmp.bin)
txt <- unlist(strsplit(tmp.char, '\r\n', fixed=TRUE))
txt
[1] "1,34,44.4,\" HIJK\032A \",99"
Update
The following better answer was posted by Duncan Murdoch to R-Devel refer. Converting it into a function I get:
sReadLines <- function(fnam) {
f <- file(fnam, "rb")
res <- readLines(f)
close(f)
res
}
I also ran into this problem when I used read.csv with a csv file that contained the SUB or CTRL-Z in the middle of the file.
Solved it with the readr package (if your file is comma separated)
library(readr)
read_csv("h3.txt")
If you have a ; as a separator, then use:
library(readr)
read_csv2("h3.txt")

Resources