Prev: Windows 7 Home Premium vs. Windows 7 Professional (using MATLAB R2007a or R2007b)
Next: extract field in excel
From: AZZA Foster on 29 Apr 2010 09:38 Thinking aboutit i little more i dont think that method will work. Basically i need to rotate the black region of the iamge so that it is square to the perimeter of the bounding box. ( But i cant use a manual rotate function, as this function has to be used on other images that may need varying degrees of rotation in order to square up the image ).
From: ImageAnalyst on 29 Apr 2010 10:23 AZZA Foster: You might try using hough() or houghlines() to try to find the average angle of the lines in the image. Sorry, I have no hough code that works with your image, but there is example code in the help. Once you find the angle, simply use imrotate to rotate it.
From: AZZA Foster on 29 Apr 2010 10:29 ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <e48a314a-8f7e-4205-9f66-d0ccfc11b4d0(a)r6g2000vbh.googlegroups.com>... > AZZA Foster: > You might try using hough() or houghlines() to try to find the average > angle of the lines in the image. Sorry, I have no hough code that > works with your image, but there is example code in the help. Once > you find the angle, simply use imrotate to rotate it. Thanks Image Analyst, i was about to go down the increment of the rotating function route, but i will try this first.
From: AZZA Foster on 29 Apr 2010 11:52 Tried it Image Analyst and it didn't work, it would of done it i had changed all the filter settings, but i need code which is a bit more robust. So after a bit of lunch and a hard think, i have come back with a method that will work automatically for ANY black&white image. What i know, the bounding box tells me the X and Y dimension of the full image. So when an object in an image is rotated to fit an image better, what always happens, the X and Y axis always shrink, as an object that is not centered to an image area will always take up a larger area than one that has been centered. COMMENT: x1 and y1 is the original size subimage bounding box. SO HERE'S MY PLAN: %% Rotate subimage by 1 degree clockwise %% Loop CW rotatedsubimage = imrotate (subimage,1) %% Rotate subimage by 1 degree clockwise %% Loop CCW rotatedsubimage = imrotate (subimage,-1) %% Place a bounding box around rotatedsubimage%% horizontalProfile = max(rotatedsubimage, [], 1); x2 = find(horizontalProfile, 1, 'first'); x3 = find(horizontalProfile, 1, 'last'); verticalProfile =max(rotatedsubimage, [], 2); y3 = find(verticalProfile, 1, 'first'); y4 = find(verticalProfile, 1, 'last'); boxX2 = [x1 x2 x2 x1 x1]; boxY2= [y1 y1 y2 y2 y1]; %% Compare, is the rotated bounding box larger or smaller than subimage(original) %% if the y2 axis is smaller than y1 axis and x2 is smaller than x1 carry on rotating%% if y2 <= y1 , x2<=x1 goto Loop CW %% goto Loop Clockwise %% if y2 is greater than y1 and x2 is greater than x1, then the image area is getting bigger,go counterclockwise instead %% if y2 > y1 , x2 > x1 goto Loop CCW %% If rotatedsubimage bounding box(y2,x2) is equal to subimage boundingbox (x1,y1) %% if y2 == y1, x2 == x1 subimage end %%carry on with rest of code %% Image Analyst will this layout work?? I need someone to sort out the loops for me, as i dont know how to make a continous loop using matlab, is it the same as C++ and assembly language software, which tend to use " for (;;)?? I dont know I know that when the image is rotated the x and y axis DO change size, as i thought matlab may clip the image automatically, but thankfully not.
From: AZZA Foster on 29 Apr 2010 12:41
Sorry above code is incorrect in terms of its method, "subimage" needs to be updated,the code below has this included. The x and y values are now also correct. K=1 %% (INITIAL CONDITION)This is set so that only a clockwise or anticlockwise function is performed in the first 2 cycles of the program loop %% %% Rotate subimage by 1 degree clockwise %% if K = 1 Loop CW rotatedsubimage = imrotate (subimage,1) %% After rotation performed update subimage bounding box%% horizontalProfile = max(subimage, [], 1); x1 = find(horizontalProfile, 1, 'first'); x2 = find(horizontalProfile, 1, 'last'); verticalProfile =max(subimage, [], 2); y1 = find(verticalProfile, 1, 'first'); y2 = find(verticalProfile, 1, 'last'); boxX = [x1 x2 x2 x1 x1]; boxY = [y1 y1 y2 y2 y1]; %% Rotate subimage by 1 degree counterclockwise %% if K = 0 Loop CCW rotatedsubimage = imrotate (subimage,-1) %% After rotation performed update subimage bounding box%% horizontalProfile = max(subimage, [], 1); x1 = find(horizontalProfile, 1, 'first'); x2 = find(horizontalProfile, 1, 'last'); verticalProfile =max(subimage, [], 2); y1 = find(verticalProfile, 1, 'first'); y2 = find(verticalProfile, 1, 'last'); boxX2 = [x1 x2 x2 x1 x1]; boxY2= [y1 y1 y2 y2 y1]; %% update subimage bounding box before performing size calculations, THEN loop %% horizontalProfile = max(rotatedsubimage, [], 1); x3 = find(horizontalProfile, 1, 'first'); x4 = find(horizontalProfile, 1, 'last'); verticalProfile =max(rotatedsubimage, [], 2); y3 = find(verticalProfile, 1, 'first'); y4 = find(verticalProfile, 1, 'last'); boxX2 = [x3 x4 x4 x3 x3]; boxY2= [y3 y3 y4 y4 y3]; %% Compare, is the rotated bounding box larger or smaller than subimage(original) %% if the y2 axis is smaller than y1 axis and x2 is smaller than x1 carry on rotating%% if y4 <= y2 , x4 <= x2 goto Loop CW %% goto Loop Clockwise %% if y2 is greater than y1 and x2 is greater than x1, then the image area is getting bigger,go counterclockwise instead %% if y4 > y2 , x4 > x2 K -1 %% K Now equals 0 keep going counterclockwise% goto Loop CCW %% If rotatedsubimage bounding box(y4,x4) is equal to subimage boundingbox (x2,y2) %% if y4 == y2, x4 == x2 subimage %% when both bounding boxs are the same size,the image is central%% end %%carry on with rest of code %% |