File: CropBlackEdges.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (59 lines) | stat: -rw-r--r-- 1,633 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function coords = CropBlackEdges(plaat)
% coords = croblackedges(image)
% returns coordinates that can be used to cut the black edges from an image
% the trick is to sum the image in the x-direction and find where the sum
% is zero, then you know where there are black edges above and below the
% image; same trick for the edges left and right by summing in the
% y-direction.
%
% COORDS = [xmin xmax ymin ymax]
%
% to cut the edges use:
% cutimage = image(coords(3):coords(4),coords(1):coords(2));
%
% DN 2007       Wrote it
% DN 2008-07-30 Now returns correct indices if one or more or all sides of
%               the image do not have a black edge
% DN 2009-02-02 Now returns error msg if all is black

plaat       = sum(plaat,3);
psychassert(any(plaat(:)),'No image in input matrix');

plaatx      = sum(plaat,2);
plaaty      = sum(plaat,1);

% y coordinates
qdiff       = diff([plaatx])~=0;
if plaatx(1)~=0 && plaatx(end)~=0
    coords(3) = 1;
    coords(4) = size(plaat,1);
else
    if plaatx(1)~=0
        coords(3) = 1;
    else
        coords(3) = find(qdiff,1,'first')+1;
    end
    if plaatx(end)~=0
        coords(4) = size(plaat,2);
    else
        coords(4) = find(qdiff,1,'last');
    end
end

% x coordinates
qdiff       = diff([plaaty])~=0;
if plaaty(1)~=0 && plaaty(end)~=0
    coords(1) = 1;
    coords(2) = size(plaat,2);
else
    if plaaty(1)~=0
        coords(1) = 1;
    else
        coords(1) = find(qdiff,1,'first')+1;
    end
    if plaaty(end)~=0
        coords(2) = size(plaat,2);
    else
        coords(2) = find(qdiff,1,'last');
    end
end