Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
imgproc
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陈皓
imgproc
Commits
a9ca035e
Commit
a9ca035e
authored
Nov 24, 2023
by
陈皓
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新
parent
4ba97838
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
326 additions
and
106 deletions
+326
-106
gateway-server/src/main/resources/application.yml
+4
-0
imgproc-server/pom.xml
+11
-0
imgproc-server/src/main/java/com/zq/imgproc/controller/ApiController.java
+1
-11
imgproc-server/src/main/java/com/zq/imgproc/controller/FileController.java
+51
-0
imgproc-server/src/main/java/com/zq/imgproc/controller/ImgProcController.java
+50
-25
imgproc-server/src/main/java/com/zq/imgproc/server/ImgProcService.java
+54
-8
imgproc-server/src/main/java/com/zq/imgproc/utils/Deskew.java
+3
-3
imgproc-server/src/main/java/com/zq/imgproc/utils/ImageUtil.java
+10
-7
imgproc-server/src/main/java/com/zq/imgproc/utils/RemoveBlackUtil.java
+28
-34
imgproc-server/src/main/java/com/zq/imgproc/utils/RemoveBlackUtil2.java
+1
-1
imgproc-server/src/main/java/com/zq/imgproc/utils/Test2.java
+57
-0
imgproc-server/src/main/java/com/zq/imgproc/utils/TestUtil.java
+0
-0
imgproc-server/src/main/java/com/zq/imgproc/vo/DetectionDTO.java
+42
-0
imgproc-server/src/main/java/com/zq/imgproc/vo/DetectionResVo2.java
+2
-5
imgproc-server/src/main/java/com/zq/imgproc/vo/SettingVO.java
+4
-4
imgproc-server/src/main/resources/application.yml
+8
-8
No files found.
gateway-server/src/main/resources/application.yml
View file @
a9ca035e
...
...
@@ -23,4 +23,8 @@ spring:
uri
:
lb://IMGPROC-SERVER
predicates
:
-
Path=/imgproc/**
-
id
:
file
uri
:
lb://IMGPROC-SERVER
predicates
:
-
Path=/file/**
imgproc-server/pom.xml
View file @
a9ca035e
...
...
@@ -92,6 +92,11 @@
<version>
5.8.19
</version>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
easyexcel
</artifactId>
<version>
3.3.2
</version>
</dependency>
<dependency>
<groupId>
com.drewnoakes
</groupId>
<artifactId>
metadata-extractor
</artifactId>
<version>
2.18.0
</version>
...
...
@@ -111,6 +116,12 @@
<artifactId>
springfox-swagger-ui
</artifactId>
<version>
2.9.2
</version>
</dependency>
<!-- guava -->
<dependency>
<groupId>
com.google.guava
</groupId>
<artifactId>
guava
</artifactId>
<version>
32.1.3-jre
</version>
</dependency>
</dependencies>
<build>
...
...
imgproc-server/src/main/java/com/zq/imgproc/controller/ApiController.java
View file @
a9ca035e
...
...
@@ -91,17 +91,7 @@ public class ApiController {
// 去黑边
RemoveBlackUtil2
.
remove
(
imgPath
,
savePath
);
// 图片弯曲矫正
HashMap
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"file"
,
FileUtil
.
file
(
savePath
));
String
response
=
HttpUtil
.
post
(
"http://ddns.gxmailu.com:18888/api/correct"
,
map
);
JSONObject
res
=
JSONUtil
.
parseObj
(
response
);
if
(
res
.
get
(
"success"
,
Boolean
.
class
))
{
String
base64
=
res
.
get
(
"data"
,
String
.
class
);
return
ResultVo
.
success
(
base64
);
}
else
{
return
ResultVo
.
fail
(
res
.
get
(
"msg"
,
String
.
class
));
}
return
ResultVo
.
success
(
Base64Encoder
.
encode
(
savePath
));
}
@ApiOperation
(
"图片灰度化"
)
...
...
imgproc-server/src/main/java/com/zq/imgproc/controller/FileController.java
0 → 100644
View file @
a9ca035e
package
com
.
zq
.
imgproc
.
controller
;
import
cn.hutool.core.util.URLUtil
;
import
com.zq.imgproc.utils.AssertUtils
;
import
io.swagger.annotations.Api
;
import
org.springframework.core.io.FileSystemResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.http.ContentDisposition
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.CrossOrigin
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.File
;
/**
* <p>
*
* </p>
*
* @author zq
* @since 2023/11/15
*/
@Api
(
tags
=
"访问文件"
)
@RestController
@CrossOrigin
@RequestMapping
(
"/"
)
public
class
FileController
{
@GetMapping
(
value
=
"/file/**"
)
public
ResponseEntity
<
Resource
>
archive
(
HttpServletRequest
request
)
{
String
requestURI
=
request
.
getRequestURI
();
requestURI
=
URLUtil
.
decode
(
requestURI
);
File
file
=
new
File
(
requestURI
);
AssertUtils
.
isTrue
(
file
.
exists
(),
"访问文件不存在"
);
String
contentDisposition
=
ContentDisposition
.
builder
(
"attachment"
)
.
filename
(
requestURI
)
.
build
().
toString
();
return
ResponseEntity
.
ok
()
.
header
(
HttpHeaders
.
CONTENT_DISPOSITION
,
contentDisposition
)
.
contentType
(
MediaType
.
APPLICATION_OCTET_STREAM
)
.
body
(
new
FileSystemResource
(
file
));
}
}
imgproc-server/src/main/java/com/zq/imgproc/controller/ImgProcController.java
View file @
a9ca035e
package
com
.
zq
.
imgproc
.
controller
;
import
cn.hutool.core.convert.Convert
;
import
cn.hutool.core.io.FileUtil
;
import
cn.hutool.extra.servlet.ServletUtil
;
import
com.google.common.cache.Cache
;
import
com.google.common.cache.CacheBuilder
;
import
com.zq.imgproc.server.ImgProcService
;
import
com.zq.imgproc.utils.AssertUtils
;
import
com.zq.imgproc.utils.DecompressUtil
;
...
...
@@ -11,6 +14,8 @@ import com.zq.imgproc.vo.*;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.io.Resource
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
...
...
@@ -18,8 +23,10 @@ import javax.servlet.http.HttpServletResponse;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.charset.StandardCharsets
;
import
java.time.Duration
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.concurrent.TimeUnit
;
/**
* <p>
...
...
@@ -35,11 +42,20 @@ import java.util.List;
@RestController
public
class
ImgProcController
{
// @Resource
// RedisUtils redisUtils;
// private final double CAST = 1;
// private final double DA = 1;
// private final double CLARITY = 10;
/**
* 普通的缓存 Cache
*/
private
final
Cache
<
String
,
Double
>
cache
=
CacheBuilder
.
newBuilder
()
.
expireAfterAccess
(
Duration
.
ofMinutes
(
30
))
.
expireAfterWrite
(
Duration
.
ofHours
(
48
))
.
maximumSize
(
1024
)
.
concurrencyLevel
(
4
)
.
initialCapacity
(
1024
)
.
build
();
private
final
double
START_VALUE
=
75
;
private
final
double
END_VALUE
=
175
;
private
final
double
CLARITY
=
2.5
;
private
final
ImgProcService
service
;
@Autowired
...
...
@@ -89,6 +105,12 @@ public class ImgProcController {
return
ResultVo
.
success
(
service
.
detection
(
pathList
));
}
@ApiOperation
(
"导出检测结果"
)
@PostMapping
(
"/getDetection"
)
public
ResponseEntity
<
Resource
>
getDetection
(
@RequestBody
List
<
ImgVO
>
pathList
)
throws
Exception
{
return
service
.
getDetection
(
pathList
);
}
@ApiOperation
(
"图片校正(deskew工具)"
)
@PostMapping
(
"/imageCorrection"
)
public
ResultVo
<?>
imageCorrection
(
@RequestBody
List
<
ImgVO
>
pathList
)
throws
IOException
{
...
...
@@ -107,26 +129,29 @@ public class ImgProcController {
return
ResultVo
.
success
(
service
.
removeBlack
(
pathList
));
}
// @ApiOperation("获取配置")
// @PostMapping("/getSetting")
// public ResultVo<?> getSetting() {
// double cast = Convert.toDouble(redisUtils.get("img-cast")) == null ? CAST : Convert.toDouble(redisUtils.get("img-cast"));
// double da = Convert.toDouble(redisUtils.get("img-da")) == null ? DA : Convert.toDouble(redisUtils.get("img-da"));
// double clarity = Convert.toDouble(redisUtils.get("img-clarity")) == null ? CLARITY : Convert.toDouble(redisUtils.get("img-clarity"));
// return ResultVo.success(SettingVO.builder().cast(cast).da(da).clarity(clarity).build());
// }
//
// @ApiOperation("设置配置")
// @PostMapping("/setSetting")
// public ResultVo<?> setSetting(@RequestBody SettingVO setting) {
// if (setting == null) {
// return ResultVo.fail("配置不能为空!");
// }
// redisUtils.setStr("img-cast", String.valueOf(setting.getCast()));
// redisUtils.setStr("img-da", String.valueOf(setting.getDa()));
// redisUtils.setStr("img-clarity", String.valueOf(setting.getClarity()));
// return ResultVo.success();
// }
@ApiOperation
(
"获取配置"
)
@GetMapping
(
"/getSetting"
)
public
ResultVo
<?>
getSetting
()
{
Double
temp1
=
cache
.
getIfPresent
(
"img-start"
);
Double
temp2
=
cache
.
getIfPresent
(
"img-end"
);
Double
temp3
=
cache
.
getIfPresent
(
"img-clarity"
);
double
start
=
temp1
==
null
?
START_VALUE
:
temp1
;
double
end
=
temp2
==
null
?
END_VALUE
:
temp2
;
double
clarity
=
temp3
==
null
?
CLARITY
:
temp3
;
return
ResultVo
.
success
(
SettingVO
.
builder
().
startValue
(
start
).
endValue
(
end
).
clarity
(
clarity
).
build
());
}
@ApiOperation
(
"设置配置"
)
@PostMapping
(
"/setSetting"
)
public
ResultVo
<?>
setSetting
(
@RequestBody
SettingVO
setting
)
{
if
(
setting
==
null
)
{
return
ResultVo
.
fail
(
"配置不能为空!"
);
}
cache
.
put
(
"img-start"
,
setting
.
getStartValue
());
cache
.
put
(
"img-end"
,
setting
.
getEndValue
());
cache
.
put
(
"img-clarity"
,
setting
.
getClarity
());
return
ResultVo
.
success
();
}
@ApiOperation
(
"图片旋转"
)
@PostMapping
(
"/rotate"
)
...
...
imgproc-server/src/main/java/com/zq/imgproc/server/ImgProcService.java
View file @
a9ca035e
...
...
@@ -6,6 +6,7 @@ import cn.hutool.core.codec.Base64Encoder;
import
cn.hutool.core.convert.Convert
;
import
cn.hutool.core.img.ImgUtil
;
import
cn.hutool.core.io.FileUtil
;
import
com.alibaba.excel.EasyExcel
;
import
com.drew.imaging.ImageMetadataReader
;
import
com.drew.metadata.Directory
;
import
com.drew.metadata.Metadata
;
...
...
@@ -23,13 +24,22 @@ import org.opencv.core.MatOfDouble;
import
org.opencv.imgcodecs.Imgcodecs
;
import
org.opencv.imgproc.Imgproc
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.core.io.InputStreamResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.http.ContentDisposition
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.imageio.ImageIO
;
import
java.awt.image.BufferedImage
;
import
java.io.BufferedInputStream
;
import
java.io.File
;
import
java.io.IOException
;
import
java.math.BigDecimal
;
import
java.math.RoundingMode
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
import
java.util.regex.Matcher
;
...
...
@@ -56,7 +66,6 @@ public class ImgProcService {
public
List
<
ImgVO
>
detection
(
List
<
ImgVO
>
pathList
)
throws
Exception
{
List
<
ImgVO
>
res
=
new
ArrayList
<>(
pathList
.
size
());
for
(
ImgVO
one
:
pathList
)
{
DetectionResVo2
vo
=
new
DetectionResVo2
();
String
path
=
one
.
getUrl
();
Mat
image
=
Imgcodecs
.
imread
(
path
);
...
...
@@ -66,13 +75,10 @@ public class ImgProcService {
// 检测图片的DPI
vo
.
setDpi
(
getDpi
(
FileUtil
.
file
(
path
)));
// 检测图片清晰度
vo
.
setClarity
(
ImageUtil
.
tenengrad
(
image
));
BigDecimal
clarity
=
BigDecimal
.
valueOf
(
ImageUtil
.
tenengrad
(
image
));
vo
.
setClarity
(
clarity
.
setScale
(
2
,
RoundingMode
.
HALF_UP
).
doubleValue
());
// 检测图片的亮度
double
[]
arr
=
brightnessDetection
(
image
);
if
(
arr
!=
null
)
{
vo
.
setCast
(
arr
[
0
]);
vo
.
setDa
(
arr
[
1
]);
}
vo
.
setBrightness
(
Convert
.
toInt
(
ImageUtil
.
brightness
(
image
)));
// 检测图片倾斜角度
vo
.
setAngle
(
getAngle
(
image
));
// 检测图片的黑边
...
...
@@ -438,7 +444,7 @@ public class ImgProcService {
index
++;
}
// 修正
return
ImageUtil
.
correct
(
pathList
)
;
return
pathList
;
}
public
List
<
ImgVO
>
deskew
(
List
<
ImgVO
>
pathList
)
{
...
...
@@ -693,5 +699,45 @@ public class ImgProcService {
return
Base64Encoder
.
encode
(
FileUtil
.
readBytes
(
dst
));
}
public
ResponseEntity
<
Resource
>
getDetection
(
List
<
ImgVO
>
pathList
)
throws
Exception
{
if
(
pathList
==
null
||
pathList
.
size
()
==
0
)
{
return
ResponseEntity
.
badRequest
().
build
();
}
pathList
=
detection
(
pathList
);
String
filePath
=
"/data/temp/"
+
UuidUtils
.
uuidNoDash
()
+
".xlsx"
;
EasyExcel
.
write
(
filePath
,
DetectionDTO
.
class
).
sheet
(
"图片检测报告"
).
doWrite
(
getData
(
pathList
));
BufferedInputStream
stream
=
FileUtil
.
getInputStream
(
filePath
);
if
(
stream
==
null
)
{
return
ResponseEntity
.
notFound
().
build
();
}
String
contentDisposition
=
ContentDisposition
.
builder
(
"attachment"
)
.
filename
(
"图片检测报告.xlsx"
)
.
build
().
toString
();
return
ResponseEntity
.
ok
()
.
header
(
HttpHeaders
.
CONTENT_DISPOSITION
,
contentDisposition
)
.
contentType
(
MediaType
.
APPLICATION_OCTET_STREAM
)
.
body
(
new
InputStreamResource
(
stream
));
}
private
List
<
DetectionDTO
>
getData
(
List
<
ImgVO
>
pathList
)
{
List
<
DetectionDTO
>
dtos
=
new
ArrayList
<>(
pathList
.
size
());
for
(
ImgVO
imgVO
:
pathList
)
{
DetectionResVo2
detectionRes
=
imgVO
.
getDetectionRes
();
DetectionDTO
dto
=
new
DetectionDTO
();
dto
.
setFilename
(
imgVO
.
getFileName
());
dto
.
setResolution
(
detectionRes
.
getWidthResolution
()
+
" x "
+
detectionRes
.
getHeightResolution
());
dto
.
setDpi
(
detectionRes
.
getDpi
());
dto
.
setBrightness
(
detectionRes
.
getBrightness
());
dto
.
setClarity
(
detectionRes
.
getClarity
());
dto
.
setAngle
(
detectionRes
.
getAngle
());
dto
.
setBlack
(
detectionRes
.
getBlack
()
?
"是"
:
"否"
);
dtos
.
add
(
dto
);
}
return
dtos
;
}
}
imgproc-server/src/main/java/com/zq/imgproc/utils/Deskew.java
View file @
a9ca035e
...
...
@@ -10,9 +10,6 @@ import java.util.HashMap;
import
java.util.List
;
import
java.util.Map
;
import
static
org
.
opencv
.
highgui
.
HighGui
.
imshow
;
import
static
org
.
opencv
.
highgui
.
HighGui
.
waitKey
;
public
class
Deskew
{
public
static
void
main
(
String
[]
args
)
{
...
...
@@ -136,6 +133,9 @@ public class Deskew {
int
w
=
image
.
cols
();
int
h
=
image
.
rows
();
if
(
angle
<
0
)
{
angle
=
360
+
angle
;
}
Point
center
=
new
Point
((
double
)
w
/
2
,
(
double
)
h
/
2
);
double
scale
=
1.0
;
...
...
imgproc-server/src/main/java/com/zq/imgproc/utils/ImageUtil.java
View file @
a9ca035e
...
...
@@ -152,14 +152,17 @@ public class ImageUtil {
// 灰度化
Mat
mat
=
Imgcodecs
.
imread
(
src
);
Mat
gray
=
mat
.
clone
();
if
(
mat
.
channels
()
==
4
||
mat
.
channels
()
==
3
)
{
// if (mat.channels() == 4 || mat.channels() == 3) {
// Imgproc.cvtColor(mat, gray, Imgproc.COLOR_BGR2GRAY);
// } else if (mat.channels() == 2) {
// Imgproc.cvtColor(mat, gray, Imgproc.COLOR_BGR5652GRAY);
// } else {
// gray = mat;
// }
Imgproc
.
cvtColor
(
mat
,
gray
,
Imgproc
.
COLOR_BGR2GRAY
);
}
else
if
(
mat
.
channels
()
==
2
)
{
Imgproc
.
cvtColor
(
mat
,
gray
,
Imgproc
.
COLOR_BGR5652GRAY
);
}
else
{
gray
=
mat
;
}
saveImage
(
gray
,
dst
);
gray
.
release
();
mat
.
release
();
}
/**
...
...
@@ -383,7 +386,7 @@ public class ImageUtil {
}
/**
* 计算图片平均亮度
* 计算图片平均亮度
,范围是0到255
*
* @param path 图片路径
* @return 图片平均亮度
...
...
imgproc-server/src/main/java/com/zq/imgproc/utils/RemoveBlackUtil.java
View file @
a9ca035e
...
...
@@ -17,14 +17,12 @@ import java.util.Random;
* @author chenhao
* @since 2023/3/8 9:31
*/
@SuppressWarnings
(
"all"
)
public
class
RemoveBlackUtil
{
public
static
void
main
(
String
[]
args
)
{
long
start
=
System
.
currentTimeMillis
();
String
testImg
=
"C:\\Users\\11419\\Desktop\\project\\company\\test\\9.jpg"
;
String
resImg
=
"C:\\Users\\11419\\Desktop\\project\\company\\test\\res9.jpg"
;
System
.
load
(
"C:\\Users\\11419\\Desktop\\project\\company\\imgproc\\lib\\opencv_java460.dll"
);
String
testImg
=
"C:\\Users\\11419\\Desktop\\test\\a.jpg"
;
String
resImg
=
"C:\\Users\\11419\\Desktop\\test\\8.jpg"
;
System
.
load
(
"D:\\project\\imgproc\\lib\\opencv_java460.dll"
);
remove
(
testImg
,
resImg
);
}
...
...
@@ -36,21 +34,21 @@ public class RemoveBlackUtil {
Mat
greyImg
=
img
.
clone
();
//1.彩色转灰色
Imgproc
.
cvtColor
(
img
,
greyImg
,
Imgproc
.
COLOR_BGR2GRAY
);
// ImageUtil.saveImage(greyImg, "C:\\Users\\11419\\Desktop\\project\\company\\test\\guoc
\\1.jpg");
ImageUtil
.
saveImage
(
greyImg
,
"C:\\Users\\11419\\Desktop\\test
\\1.jpg"
);
Mat
gaussianBlurImg
=
greyImg
.
clone
();
// 2.高斯滤波,降噪
Imgproc
.
GaussianBlur
(
greyImg
,
gaussianBlurImg
,
new
Size
(
3
,
3
),
2
,
2
);
// ImageUtil.saveImage(gaussianBlurImg, "C:\\Users\\11419\\Desktop\\project\\company\\test\\guoc\\2.jpg");
Mat
cannyImg
=
gaussianBlurImg
.
clone
();
Imgproc
.
GaussianBlur
(
greyImg
,
gaussianBlurImg
,
new
Size
(
3
,
3
),
0
);
ImageUtil
.
saveImage
(
greyImg
,
"C:\\Users\\11419\\Desktop\\test\\2.jpg"
);
// 3.Canny边缘检测
Imgproc
.
Canny
(
gaussianBlurImg
,
cannyImg
,
20
,
60
,
3
,
false
);
// ImageUtil.saveImage(cannyImg, "C:\\Users\\11419\\Desktop\\project\\company\\test\\guoc\\3.jpg");
Mat
cannyImg
=
gaussianBlurImg
.
clone
();
Imgproc
.
Canny
(
gaussianBlurImg
,
cannyImg
,
50
,
200
);
ImageUtil
.
saveImage
(
cannyImg
,
"C:\\Users\\11419\\Desktop\\test\\3.jpg"
);
// 4.膨胀,连接边缘
Mat
dilateImg
=
cannyImg
.
clone
();
Imgproc
.
dilate
(
cannyImg
,
dilateImg
,
new
Mat
(),
new
Point
(-
1
,
-
1
),
2
,
1
,
new
Scalar
(
1
));
// ImageUtil.saveImage(dilateImg, "C:\\Users\\11419\\Desktop\\project\\company\\test\\guoc\\4.jpg");
Imgproc
.
dilate
(
cannyImg
,
dilateImg
,
new
Mat
(),
new
Point
(-
1
,
-
1
),
3
,
1
,
new
Scalar
(
1
));
ImageUtil
.
saveImage
(
dilateImg
,
"C:\\Users\\11419\\Desktop\\test\\4.jpg"
);
//5.对边缘检测的结果图再进行轮廓提取
List
<
MatOfPoint
>
contours
=
new
ArrayList
<>();
List
<
MatOfPoint
>
drawContours
=
new
ArrayList
<>();
...
...
@@ -64,8 +62,7 @@ public class RemoveBlackUtil {
MatOfPoint2f
approx
=
new
MatOfPoint2f
();
approx
.
convertTo
(
approx
,
CvType
.
CV_32F
);
for
(
int
i
=
0
;
i
<
contours
.
size
();
i
++)
{
MatOfPoint
contour
=
contours
.
get
(
i
);
for
(
MatOfPoint
contour
:
contours
)
{
// 边框的凸包
Imgproc
.
convexHull
(
contour
,
hull
);
// 用凸包计算出新的轮廓点
...
...
@@ -78,7 +75,7 @@ public class RemoveBlackUtil {
MatOfPoint2f
contourHull
=
new
MatOfPoint2f
();
contourHull
.
fromList
(
newPoints
);
// 多边形拟合凸包边框(此时的拟合的精度较低)
Imgproc
.
approxPolyDP
(
contourHull
,
approx
,
Imgproc
.
arcLength
(
contourHull
,
true
)
*
0.02
,
true
);
Imgproc
.
approxPolyDP
(
contourHull
,
approx
,
Imgproc
.
arcLength
(
contourHull
,
true
)
*
0.02
,
true
);
MatOfPoint
mat
=
new
MatOfPoint
();
mat
.
fromArray
(
approx
.
toArray
());
drawContours
.
add
(
mat
);
...
...
@@ -89,7 +86,7 @@ public class RemoveBlackUtil {
Imgproc
.
isContourConvex
(
approxf1
))
{
double
maxCosine
=
0
;
for
(
int
j
=
2
;
j
<
5
;
j
++)
{
double
cosine
=
Math
.
abs
(
getAngle
(
approxf1
.
toArray
()[
j
%
4
],
approxf1
.
toArray
()[
j
-
2
],
approxf1
.
toArray
()[
j
-
1
]));
double
cosine
=
Math
.
abs
(
getAngle
(
approxf1
.
toArray
()[
j
%
4
],
approxf1
.
toArray
()[
j
-
2
],
approxf1
.
toArray
()[
j
-
1
]));
maxCosine
=
Math
.
max
(
maxCosine
,
cosine
);
}
// 角度大概72度
...
...
@@ -103,38 +100,35 @@ public class RemoveBlackUtil {
}
//这里是把提取出来的轮廓通过不同颜色的线描述出来,具体效果可以自己去看
Random
r
=
new
Random
();
//
for (int i = 0; i < drawContours.size(); i++) {
//
Imgproc.drawContours(linePic, drawContours, i, new Scalar(r.nextInt(255),r.nextInt(255), r.nextInt(255)));
//
}
// ImageUtil.saveImage(linePic, "C:\\Users\\11419\\Desktop\\project\\company\\test\\guoc
\\5.jpg");
for
(
int
i
=
0
;
i
<
drawContours
.
size
();
i
++)
{
Imgproc
.
drawContours
(
linePic
,
drawContours
,
i
,
new
Scalar
(
r
.
nextInt
(
255
),
r
.
nextInt
(
255
),
r
.
nextInt
(
255
)));
}
ImageUtil
.
saveImage
(
linePic
,
"C:\\Users\\11419\\Desktop\\test
\\5.jpg"
);
//7.找出最大的矩形
int
index
=
findLargestSquare
(
squares
);
MatOfPoint
largest_square
=
null
;
if
(
squares
!=
null
&&
squares
.
size
()
>
0
){
MatOfPoint
largest_square
;
if
(
squares
.
size
()
>
0
){
largest_square
=
squares
.
get
(
index
);
}
else
{
System
.
out
.
println
(
"图片无法识别"
);
return
;
}
Mat
polyPic
=
Mat
.
zeros
(
img
.
size
(),
CvType
.
CV_8UC3
);
//
Imgproc.drawContours(polyPic, squares, index, new Scalar(0, 0,255), 2);
// ImageUtil.saveImage(polyPic, "C:\\Users\\11419\\Desktop\\project\\company\\test\\guoc
\\6.jpg");
Imgproc
.
drawContours
(
polyPic
,
squares
,
index
,
new
Scalar
(
0
,
0
,
255
),
2
);
ImageUtil
.
saveImage
(
polyPic
,
"C:\\Users\\11419\\Desktop\\test
\\6.jpg"
);
//存储矩形的四个凸点
hull
=
new
MatOfInt
();
Imgproc
.
convexHull
(
largest_square
,
hull
,
false
);
List
<
Integer
>
hullList
=
hull
.
toList
();
List
<
Point
>
polyContoursList
=
largest_square
.
toList
();
List
<
Point
>
hullPointList
=
new
ArrayList
<>();
List
<
Point
>
lastHullPointList
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
hullList
.
size
();
i
++){
Imgproc
.
circle
(
polyPic
,
polyContoursList
.
get
(
hullList
.
get
(
i
)),
10
,
new
Scalar
(
r
.
nextInt
(
255
),
r
.
nextInt
(
255
),
r
.
nextInt
(
255
),
3
));
hullPointList
.
add
(
polyContoursList
.
get
(
hullList
.
get
(
i
)));
for
(
Integer
integer
:
hullList
)
{
Imgproc
.
circle
(
polyPic
,
polyContoursList
.
get
(
integer
),
10
,
new
Scalar
(
r
.
nextInt
(
255
),
r
.
nextInt
(
255
),
r
.
nextInt
(
255
),
3
));
hullPointList
.
add
(
polyContoursList
.
get
(
integer
));
}
Core
.
addWeighted
(
polyPic
,
1
,
img
,
1
,
0
,
img
);
// ImageUtil.saveImage(img, "C:\\Users\\11419\\Desktop\\project\\company\\test\\guoc\\7.jpg");
for
(
int
i
=
0
;
i
<
hullPointList
.
size
();
i
++){
lastHullPointList
.
add
(
hullPointList
.
get
(
i
));
}
ImageUtil
.
saveImage
(
img
,
"C:\\Users\\11419\\Desktop\\test\\7.jpg"
);
List
<
Point
>
lastHullPointList
=
new
ArrayList
<>(
hullPointList
);
//dstPoints储存的是变换后各点的坐标,依次为左上,右上,右下, 左下
//srcPoints储存的是上面得到的四个角的坐标
Point
[]
dstPoints
=
{
new
Point
(
0
,
0
),
new
Point
(
img
.
cols
(),
0
),
new
Point
(
img
.
cols
(),
img
.
rows
()),
new
Point
(
0
,
img
.
rows
())};
...
...
imgproc-server/src/main/java/com/zq/imgproc/utils/RemoveBlackUtil2.java
View file @
a9ca035e
...
...
@@ -18,7 +18,7 @@ public class RemoveBlackUtil2 {
/**
* 去黑边"全黑"阈值
*/
private
static
final
Integer
BLACK_VALUE
=
2
00
;
private
static
final
Integer
BLACK_VALUE
=
1
00
;
public
static
void
remove
(
String
src
,
String
dst
)
{
Mat
mat
=
Imgcodecs
.
imread
(
src
);
...
...
imgproc-server/src/main/java/com/zq/imgproc/utils/Test2.java
0 → 100644
View file @
a9ca035e
package
com
.
zq
.
imgproc
.
utils
;
import
org.opencv.core.Core
;
import
org.opencv.core.CvType
;
import
org.opencv.core.Mat
;
import
org.opencv.imgcodecs.Imgcodecs
;
import
org.opencv.imgproc.Imgproc
;
import
java.io.IOException
;
/**
* <p>
*
* </p>
*
* @author chenhao
* @since 2023/11/20
*/
public
class
Test2
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
System
.
load
(
"D:\\project\\imgproc\\lib\\opencv_java460.dll"
);
String
src
=
"C:\\Users\\11419\\Desktop\\Deskew\\TestImages\\6.png"
;
String
dst
=
"C:\\Users\\11419\\Desktop\\Deskew\\TestImages\\6res.png"
;
// 读取输入图像
Mat
inputImage
=
Imgcodecs
.
imread
(
src
);
// 将图像转换为灰度图像
Mat
grayImage
=
new
Mat
();
Imgproc
.
cvtColor
(
inputImage
,
grayImage
,
Imgproc
.
COLOR_BGR2GRAY
);
// 对灰度图像进行高斯模糊处理
Mat
blurredImage
=
new
Mat
();
Imgproc
.
GaussianBlur
(
grayImage
,
blurredImage
,
new
org
.
opencv
.
core
.
Size
(
3
,
3
),
0
);
// 使用拉普拉斯算子进行边缘检测
Mat
laplacianImage
=
new
Mat
();
Imgproc
.
Laplacian
(
blurredImage
,
laplacianImage
,
CvType
.
CV_16S
,
3
,
1
,
0
,
Core
.
BORDER_DEFAULT
);
Core
.
convertScaleAbs
(
laplacianImage
,
laplacianImage
);
// 对滤波后的图像进行阈值处理,找到孤立点
double
threshold
=
30.0
;
Mat
binaryImage
=
new
Mat
();
Imgproc
.
threshold
(
laplacianImage
,
binaryImage
,
threshold
,
255
,
Imgproc
.
THRESH_BINARY
);
// 计算检测到的孤立点数量
int
isolatedPointCount
=
Core
.
countNonZero
(
binaryImage
);
// 输出孤立点数量
System
.
out
.
println
(
"检测到的孤立点数量:"
+
isolatedPointCount
);
// 可以在结果图像上标记检测到的孤立点,或者保存结果图像以便查看
Imgcodecs
.
imwrite
(
dst
,
binaryImage
);
}
}
imgproc-server/src/main/java/com/zq/imgproc/utils/TestUtil.java
View file @
a9ca035e
This diff is collapsed.
Click to expand it.
imgproc-server/src/main/java/com/zq/imgproc/vo/DetectionDTO.java
0 → 100644
View file @
a9ca035e
package
com
.
zq
.
imgproc
.
vo
;
import
com.alibaba.excel.annotation.ExcelIgnore
;
import
com.alibaba.excel.annotation.ExcelProperty
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
import
lombok.EqualsAndHashCode
;
/**
* <p>
*
* </p>
*
* @author chenhao
* @since 2023/11/24
*/
@Data
@EqualsAndHashCode
public
class
DetectionDTO
{
@ExcelProperty
(
"文件名称"
)
private
String
filename
;
@ExcelProperty
(
"分辨率"
)
private
String
resolution
;
@ExcelProperty
(
"DPI"
)
private
Integer
dpi
;
@ExcelProperty
(
"平均亮度值"
)
private
double
brightness
;
@ExcelProperty
(
"清晰度值"
)
private
double
clarity
;
@ExcelProperty
(
"倾斜角度"
)
private
double
angle
;
@ExcelProperty
(
"黑边情况"
)
private
String
black
;
}
imgproc-server/src/main/java/com/zq/imgproc/vo/DetectionResVo2.java
View file @
a9ca035e
...
...
@@ -27,11 +27,8 @@ public class DetectionResVo2 {
@ApiModelProperty
(
"图片的DPI"
)
private
Integer
dpi
;
@ApiModelProperty
(
"亮度值"
)
private
double
cast
;
@ApiModelProperty
(
"亮度异常值"
)
private
double
da
;
@ApiModelProperty
(
"平均亮度值"
)
private
double
brightness
;
@ApiModelProperty
(
"图片的清晰度指标,值越大越清晰"
)
private
double
clarity
;
...
...
imgproc-server/src/main/java/com/zq/imgproc/vo/SettingVO.java
View file @
a9ca035e
...
...
@@ -20,11 +20,11 @@ import lombok.NoArgsConstructor;
@Builder
public
class
SettingVO
{
@ApiModelProperty
(
"亮度值"
)
private
double
cast
;
@ApiModelProperty
(
"亮度值
起始范围
"
)
private
double
startValue
;
@ApiModelProperty
(
"亮度
异常值
"
)
private
double
da
;
@ApiModelProperty
(
"亮度
值结束范围
"
)
private
double
endValue
;
@ApiModelProperty
(
"图片的清晰度指标,值越大越清晰"
)
private
double
clarity
;
...
...
imgproc-server/src/main/resources/application.yml
View file @
a9ca035e
...
...
@@ -18,12 +18,12 @@ spring:
ip
:
local-parsing
:
true
#imgconfig:
# opencv: /opt/services/tianjin-backend/lib/opencv_java460.so
# deskew: /opt/services/tianjin-backend/lib/Deskew/Bin/deskew
# deskewpy: /opt/tianjin/lib/correct.py
imgconfig
:
opencv
:
D:/project/imgproc/lib/opencv_java460.dll
deskew
:
C:/Users/11419/Desktop/Deskew/Bin/deskew.exe
deskewpy
:
D:/project/imgproc/lib/correct.py
opencv
:
/opt/tianjin/lib/opencv_java460.so
deskew
:
/opt/tianjin/lib/Deskew/Bin/deskew
deskewpy
:
/opt/tianjin/lib/correct.py
#imgconfig:
# opencv: D:/project/imgproc/lib/opencv_java460.dll
# deskew: C:/Users/11419/Desktop/Deskew/Bin/deskew.exe
# deskewpy: D:/project/imgproc/lib/correct.py
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment