github.com/kaydxh/golang@v0.0.131/pkg/gocv/rect_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package gocv_test
    23  
    24  import (
    25  	"fmt"
    26  	"testing"
    27  
    28  	gocv_ "github.com/kaydxh/golang/pkg/gocv"
    29  )
    30  
    31  func TestScale(t *testing.T) {
    32  	testCases := []struct {
    33  		X      int32
    34  		Y      int32
    35  		Width  int32
    36  		Height int32
    37  		factor float32
    38  	}{
    39  		{
    40  			X:      100,
    41  			Y:      100,
    42  			Width:  100,
    43  			Height: 100,
    44  			factor: 1.1,
    45  		},
    46  		{
    47  			X:      100,
    48  			Y:      100,
    49  			Width:  100,
    50  			Height: 100,
    51  			factor: 0.9,
    52  		},
    53  	}
    54  
    55  	for i, testCase := range testCases {
    56  		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
    57  			r := gocv_.Rect{
    58  				X:      testCase.X,
    59  				Y:      testCase.Y,
    60  				Width:  testCase.Width,
    61  				Height: testCase.Height,
    62  			}
    63  			r2 := r.Scale(testCase.factor).Intersect(r)
    64  			t.Logf("r2: %v", r2)
    65  		})
    66  	}
    67  }
    68  
    69  func TestClosest(t *testing.T) {
    70  	testCases := []struct {
    71  		r  gocv_.Rect
    72  		rs []gocv_.Rect
    73  	}{
    74  		/*
    75  			{
    76  				r: gocv_.Rect{
    77  					X:      1042,
    78  					Y:      518,
    79  					Width:  389,
    80  					Height: 467,
    81  				},
    82  				rs: []gocv_.Rect{
    83  					// out
    84  					{
    85  						X:      0,
    86  						Y:      10,
    87  						Width:  10,
    88  						Height: 10,
    89  					},
    90  					{
    91  						X:      400,
    92  						Y:      600,
    93  						Width:  700,
    94  						Height: 700,
    95  					},
    96  
    97  					// small in
    98  					{
    99  						X:      1100,
   100  						Y:      600,
   101  						Width:  300,
   102  						Height: 300,
   103  					},
   104  
   105  					//big in
   106  					{
   107  						X:      938,
   108  						Y:      245,
   109  						Width:  807,
   110  						Height: 819,
   111  					},
   112  				},
   113  			},
   114  		*/
   115  		{
   116  			r: gocv_.Rect{
   117  				X:      1031,
   118  				Y:      728,
   119  				Width:  561,
   120  				Height: 340,
   121  			},
   122  			rs: []gocv_.Rect{
   123  				//big in
   124  				{
   125  					X:      938,
   126  					Y:      244,
   127  					Width:  808,
   128  					Height: 820,
   129  				},
   130  			},
   131  		},
   132  	}
   133  
   134  	for i, testCase := range testCases {
   135  		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
   136  			area, cr := testCase.r.Closest(testCase.rs...)
   137  			t.Logf("cr: %v, area: %v", cr, area)
   138  		})
   139  	}
   140  }