# bug #48943: Processing image freezes OS

 Submitted by: Sasitha Iresh Submitted on: Mon 29 Aug 2016 05:54:19 PM UTC Category: Performance Severity: 3 - Normal Priority: 5 - Normal Item Group: Performance Status: Invalid Assigned to: None Originator Name: Sasitha Iresh Open/Closed: Closed Release: 4.0.3 Operating System: Microsoft Windows

You are not logged in

## Discussion

Thu 01 Sep 2016 07:02:08 PM UTC, comment #7:

Dan, the image package also has an imhist function that does that, including plotting the histogram. It is also aware of the different numeric limits of each data type.

CarnĂ« Draug <carandraug>
Thu 01 Sep 2016 11:24:12 AM UTC, comment #6:

The reason why Matlab does it faster is because it has JIT compiling but Octave does not. See http://stackoverflow.com/a/38724963/1609556

CarnĂ« Draug <carandraug>
Thu 01 Sep 2016 06:00:55 AM UTC, comment #5:

Give the attached variation on your script file a try.  As a comparison, I'll first reduce the image size:

octave:43> myimg = uint8(2^8*rand(160,256,3));
octave:44> figure; tic; C = histro(myimg); toc
Elapsed time is 4.88092 seconds.
octave:45> figure; tic; C2 = histro2(myimg); toc
Elapsed time is 0.0739899 seconds.

So that is a factor of fifty times faster?  I note that the two histograms do not match in the last places:

octave:46> [C(end-7:end); C2(end-7:end)]
ans =

506    448    473    478    510    486   1207      0
506    448    473    478    510    486    477    730

and the reason is because in your formula:

C(img(i,j)+1) = C(img(i,j)+1)+1;

img(i,j) is an uint8 and the largest value an uint8 can be is 255.  img(i,j)+1 is also an uint8, hence

img(i,j)=254 results in img(i,j)+1=255
img(i,j)=255 results in img(i,j)+1=255

i.e.,

octave:48> uint8(255)+1
ans = 255

So, let me run the larger image in just the new histro2.m script:

octave:49> myimg = uint8(2^8*rand(1600,2560,3));
octave:50> figure; tic; C2 = histro2(myimg); toc
Elapsed time is 2.20311 seconds.

2.2 seconds.  That's pretty fast.

I'll also note that if you are passing in an image that has three planes, I don't see how that comes out to be a "Gray Image" histogram.  Both of these histogram scripts include every element of every plane.

Dan Sebald <sebald>
Thu 01 Sep 2016 04:37:20 AM UTC, comment #4:

I uploaded the .m file of this function.

I failed to mention in the report that this same function work in few seconds in Matlab. Therefore i doubt that there is much huge problem with memory allocation and optimization.

Sasitha Iresh <sasithairesh>
Tue 30 Aug 2016 05:23:18 AM UTC, comment #3:

Make that "tenets".

Dan Sebald <sebald>
Tue 30 Aug 2016 05:19:39 AM UTC, comment #2:

Freezing the OS isn't good, but that might be an OS problem as a program going outside its memory space or something as bad shouldn't be a problem with modern operating systems.  (If you can identify a crash though and any sort of message right before it happens, that would be good.)

Anyway, what you are doing violates some of the main tenants of interpreted language processing.

1) Pre-assign memory.  If your count vector C() (a matrix really seeing the image has three color planes) is not pre-assigned, every time an image value greater than previous appears the whole vector has to be reallocated to a bigger memory space to make room.  And if you have an 8-bit image with 3 planes, statistically speaking that could be a lot of reallocations.  And this sort of memory exercising might be what is testing your OS.  For what it's worth, Matlab used to be slow at this too, but improved over the years.  (I think there are ways internally that could improve this, and it's been discussed at OctConf sessions, but no one's implemented it.)  In any case, pre-allocate memory.

2) Avoid indexing as much as possible.  That has to be interpretted and is about ten times slower than inherent indexing through matrix multiplication, sorting routines, logical indexing, etc.  There is a lot to learn about the efficient ways of using the language.  As AW suggests, look for pre-existing functions that are already optimized.

Dan Sebald <sebald>
Mon 29 Aug 2016 06:31:59 PM UTC, comment #1:

Please create a complete scripts which can be run. Your code is incomplete and it isn't obvious what you are trying to do but I'm very sure you can vectorize it or there is already a function in the image package which does what you want to archive.

Andreas Weber <andy1978>
Mon 29 Aug 2016 05:54:19 PM UTC, original submission:

I tried manually created histogram function to process images with
only two loops. It take more than 15 minutes to get results from
octave and sometimes it freezes the OS. However i tried same in Matlab
and it gives results quickly.

used image is unit8 1600x2560x3 image.

part of the code is:

[m,n] = size(img);

for i = 1:m
for j= 1:n
C(img(i,j)+1) = C(img(i,j)+1)+1;
K = K+1;
end
end

Sasitha Iresh <sasithairesh>

## Attached Files

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

Comment:

Attached Files
file #38399:  histro2.m added by sebald (137B - text/x-objcsrc)
file #38398:  histro.m added by sasithairesh (240B - application/octet-stream)

## Dependencies

Depends on the following items: None found

Items that depend on this one: None found

Carbon-Copy List
• -email is unavailable- added by sebald (Posted a comment)
• -email is unavailable- added by andy1978 (Posted a comment)
• -email is unavailable- added by sasithairesh (Submitted the item)
•

Do you think this task is very important?
This task has 0 encouragements so far.

Only project members can vote.