Notifications
Clear all

[Closed] Working with pixels

hi
im trying to get a location of one pixel in a bitmap. the location will be the pixel number on the bmp_w, and the pixel number on the bmp_h. the pixel will be the pixel with the highest value. so ill get something like:
the_w = 13
the_h = 55
how can i update the_w&the_h while to for loop is checking the pixels?

thanx



--the bitmap
work_bmp = openbitmap "C:\Users\User\Downloads\New folder\12.jpg"

--read the width of the bitmap into a variable
bmp_w = work_bmp.width
bmp_h = work_bmp.height


for h = 1 to bmp_h do 
--loop through all rows in the image
	(
	pixel_line = getpixels work_bmp [0,(h-1)] bmp_w
	--store a single row #h into an array
	

	for w = 1 to bmp_w do 
	--loop through all pixels in the current row
		(
		size_value = (pixel_line[w].r+pixel_line[w].g+pixel_line[w].b)/3
		--calculate a value based on the luminosity (R+G+B)/3 of the pixel. 
	    if size_value > final_size_value -- chack if the corrent pixel has the highest value
			then
		(
			final_size_value = size_value -- update the final_size_value
			the_w = w[i] --update the w row number
			the_h = h[i] --update the h row number
		)
		
		) --end of w loop
		
	)--end h loop

	print final_size_value --pixal with the brightest value
	print the_w -- the row were the final_size_value found on bmp_w
	print the_h -- the row were the final_size_value found on bmp_h
	

6 Replies

Try this:

(
  	
  	fn GetHighestValuePixel filename =
  	(
  		img = openbitmap filename
  		
  		w = img.width
  		h = img.height
  		
  		found = false
  		pixel = [0,0]
  		val = 0.0		
  		
  		for y = 0 to h-1 while not found do
  		(
  			row = getpixels img [0,y] w
  			for x = 1 to row.count do
  			(
  				pixelValue = row[x].v
  				if pixelValue > val do
  				(
  					pixel = [x-1,y]
  					val = pixelValue
  					if val == 255 do exit with found = true
  				)
  			)
  		)
  		
  		close img
  		
  		return #(pixel, val)
  	)
  
  	GetHighestValuePixel @"C:\BITMAP.BMP"    
  )

thanx PolyTools3d for the help:)

i have some questions about what you did…

(
  	
  	fn GetHighestValuePixel filename =
  	(
  		img = openbitmap filename
  		
  		w = img.width
  		h = img.height
  		
  		found = false
  		pixel = [0,0]
  		val = 0.0		
  		
  		for y = 0 to h-1 while not found do
  		(
  			row = getpixels img [0,y] w
  			for x = 1 to row.count do
  			(
  				pixelValue = row[x].v
				/*
				what is the ".v " mean?
				*/
  				if pixelValue > val do
  				(
  					pixel = [x-1,y]    
					/*
					did you use -1 because of the "for loop" count start in
					1 while the 1th pixel line is 0?
					*/
  					val = pixelValue
  					if val == 255 do exit with found = true
  				)
  			)
  		)
  		
  		close img
  		
  		return #(pixel, val) -- why i need this one?
  	)
  
  	GetHighestValuePixel @"C:\BITMAP.BMP" -- why didn`t you use  "img"?
  )

img is used. It uses the filename you send to the function as a parameter. Maybe you are confused by the surrounding brackets?

  • what is the “.v ” mean?
    .v or value is one of the components of a color. The other two are hue and saturation. Take a look here.

  • did you use -1 because of the “for loop” count start in 1 while the 1th pixel line is 0?
    Yes.

  • why i need this one? return #(pixel, val)
    This is the returned value of the function. It returns an array with the pixel location (x,y) and its value

  • why didn`t you use “img”? GetHighestValuePixel @“C:\BITMAP.BMP”
    The bitmap filename is sent as a parameter to the function, so it can open the bitmap.
    It can be changed to accept a bitmap value instead if you need. I just used a filename so it was more convinient to open/close the bitmap from the function and not outside it.

I forgot to mention that the function will end as soon as a pixel with value 255 (maximum) is found. So if the first pixel [0,0] has a value of 255 that will be the returned pixel, regardless of if there are other pixels with the same value in the image.

The function can be modified to return the latest pixel found with a value of 255 if you need.

If you know that the image has a unique pixel with the highest value, I would keep the function as is though, as if the pixel with a value of 255 is not at the end of the image, the function will return earlier, making it faster.