如何在图像中创建边缘检测滤镜?
How can I create a edge detection filter in the image?
Swift 5.0
我有一项任务是在图像中创建这种类型的输出,以了解如何应用带有渐变的边框和边框颜色。请在下面查看为我工作的相同任务的图像。
查看原图
我需要这个输出
试试看 Swift 5
pod 'GPUImage'
UIImagePickerControllerDelegate , UINavigationControllerDelegate
import GPUImage
@IBAction func selectPhotoButtonClick(_ sender: UIButton) {
let picker = UIImagePickerController.init()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .photoLibrary
self.present(picker, animated: true, completion: nil)
}
@IBAction func detectEdgeButtonClick(_ sender: Any) {
self.GPUResult()
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let img = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
self.dismiss(animated: true) {
self.ivImage.image = img!
self.uploadImage(img: img!)
}
}
func uploadImage(img : UIImage) {
let heder = ["User-Agent" : "Dalvik/2.1.0 (Linux; U; Android 5.1; 1201 Build/LMY47I)",
"Host" : "prodml.dexati.com",
"Connection" : "Keep-Alive",
"Accept-Encoding" : "gzip"]
let imgData = img.jpegData(compressionQuality: 1.0)
Alamofire.upload(
multipartFormData: { MultipartFormData in
MultipartFormData.append(imgData!, withName: "file", fileName: "file.jpeg", mimeType: "image/jpeg")
}, to: "http://prodml.dexati.com/DexNewGrabcut", method: .post, headers: heder)
{ (result) in
switch result {
case .success(let response, _, _):
response.responseData(completionHandler: { (DataResponse) in
if DataResponse.result.value != nil {
if let i = UIImage.init(data: DataResponse.result.value!) {
let maskLayer = CALayer()
let mask = i
maskLayer.contents = mask.cgImage
maskLayer.contentsGravity = .resizeAspect
maskLayer.frame = self.ivImage.frame
self.viewMain.layer.mask = maskLayer
self.inputImage = self.ivImage.image
}
else
{
print("error63==>",DataResponse.error?.localizedDescription)
}
} else
{
print("error67==>",DataResponse.error?.localizedDescription)
}
})
case .failure(let encodingError): break
}
}
}
func GPUResult(){
var image_p: GPUImagePicture?
if let anImage = inputImage {
image_p = GPUImagePicture(image: anImage)
}
let filter1 = GPUImageCannyEdgeDetectionFilter()//GPUImageSobelEdgeDetectionFilter()
image_p?.addTarget(filter1)
image_p?.processImage()
let outputImage = filter1.newCGImage(byFilteringImage: inputImage)
let finalImage = UIImage(cgImage: (outputImage?.takeRetainedValue())!)
ivImage.image = finalImage
}
Swift 5.0
我有一项任务是在图像中创建这种类型的输出,以了解如何应用带有渐变的边框和边框颜色。请在下面查看为我工作的相同任务的图像。
查看原图
我需要这个输出
试试看 Swift 5
pod 'GPUImage'
UIImagePickerControllerDelegate , UINavigationControllerDelegate
import GPUImage
@IBAction func selectPhotoButtonClick(_ sender: UIButton) {
let picker = UIImagePickerController.init()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .photoLibrary
self.present(picker, animated: true, completion: nil)
}
@IBAction func detectEdgeButtonClick(_ sender: Any) {
self.GPUResult()
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let img = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
self.dismiss(animated: true) {
self.ivImage.image = img!
self.uploadImage(img: img!)
}
}
func uploadImage(img : UIImage) {
let heder = ["User-Agent" : "Dalvik/2.1.0 (Linux; U; Android 5.1; 1201 Build/LMY47I)",
"Host" : "prodml.dexati.com",
"Connection" : "Keep-Alive",
"Accept-Encoding" : "gzip"]
let imgData = img.jpegData(compressionQuality: 1.0)
Alamofire.upload(
multipartFormData: { MultipartFormData in
MultipartFormData.append(imgData!, withName: "file", fileName: "file.jpeg", mimeType: "image/jpeg")
}, to: "http://prodml.dexati.com/DexNewGrabcut", method: .post, headers: heder)
{ (result) in
switch result {
case .success(let response, _, _):
response.responseData(completionHandler: { (DataResponse) in
if DataResponse.result.value != nil {
if let i = UIImage.init(data: DataResponse.result.value!) {
let maskLayer = CALayer()
let mask = i
maskLayer.contents = mask.cgImage
maskLayer.contentsGravity = .resizeAspect
maskLayer.frame = self.ivImage.frame
self.viewMain.layer.mask = maskLayer
self.inputImage = self.ivImage.image
}
else
{
print("error63==>",DataResponse.error?.localizedDescription)
}
} else
{
print("error67==>",DataResponse.error?.localizedDescription)
}
})
case .failure(let encodingError): break
}
}
}
func GPUResult(){
var image_p: GPUImagePicture?
if let anImage = inputImage {
image_p = GPUImagePicture(image: anImage)
}
let filter1 = GPUImageCannyEdgeDetectionFilter()//GPUImageSobelEdgeDetectionFilter()
image_p?.addTarget(filter1)
image_p?.processImage()
let outputImage = filter1.newCGImage(byFilteringImage: inputImage)
let finalImage = UIImage(cgImage: (outputImage?.takeRetainedValue())!)
ivImage.image = finalImage
}